Slajni Slajni - 2 years ago 412
C# Question

C# method prototype like in c++

Can I somehow make a prototype of method and declare its body in c# like I do in c++?
For Example in

C++
:

void foo();

int main()
{
foo();
return 0;
}

void foo(){
cout << "im foo";
}


How can I achieve it in c#?

Answer Source

There are partial methods in C#, which I think is quite similar.

Partial methods must be private and return void. If your methods are not private or do not return void, consider using the second approach below.

Here's an example of a partial method:

partial class MyClass {
    partial void Foo();
}

// This can be in another file
partial class MyClass  {
    partial void Foo() {
        Console.WriteLine("Foo");
    }
}

Another approach is to use interfaces:

interface IFoo {
    void Foo();
}

class MyClass: IFoo {
    public void Foo() {
        Console.WriteLine("Foo");
    }
}

However, if you just want to see all the methods in a class in a neat way, Visual Studio can already do that for you. I don't have Visual Studio on the computer that I'm currently using so here's a screenshot I found on Google:

enter image description here

The part where it says "RunFile(PythonEngine engine...)" is where you want to click. After you clicked All the methods in the current file will be displayed. You can click on one of them to jump to it!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download