wondernate
wondernate

Reputation: 344

How can I declare a method then define it later?

In C# is there a way to declare the class then define it later? I really like in C++ where I can list all the methods at the top like a TOC then define everything later.

Can that be done is C#?

I have used the idea of defining a method that just runs a similarly named method in it then the similar method is at the bottom. but I am thinking there is a better way and googling returns a bunch of basic code on creating classes with no answer.

so here is what I do...

...
public void methodA(){methodAcontent()};
public void methodB()...etc...

...further down...

private void methodAcontent(){
...All the code..
}

is there a better way?

Upvotes: 3

Views: 3356

Answers (5)

lesscode
lesscode

Reputation: 6361

If you're doing this as a means to "document" the public interface to a class that's properly encapsulating a concept or object in your problem domain, then use an interface.

If you're doing it as a means to get an "overview" the structure of a class, then Visual Studio has several ways to give you this. You can collapse the code to just its definitions (Ctrl+M, O), or look at the Class View (Ctrl+W, C).

Upvotes: 0

Euphoric
Euphoric

Reputation: 12849

Why would you need that?

C# is using multipass compilation, so it doesn't matter where the function is defined and when used. You can have function defined at end of the class and use it in the beginning and it will still compile fine.

Also IDE helps you with that. You have ability to collapse bodies of all methods, there is list of all methods in one combobox and InteliSense is extremly helpful in finding correct methods.

And using practices from C++ in C# is really bad idea, because both are quite different in how they solve the problems.

Upvotes: 0

Mike Zboray
Mike Zboray

Reputation: 40818

There's not a good way to do this in the C# language, but the Visual Studio IDE can collapse a file to its definitions which you can then expand individually (see this). This along with code regions helps me organize longer files.

Upvotes: 0

Tigran
Tigran

Reputation: 62246

Upvotes: 0

burning_LEGION
burning_LEGION

Reputation: 13450

this like Interface http://msdn.microsoft.com/en-us/library/87d83y5b.aspx

Upvotes: 1

Related Questions