Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# class default method

Is there a way to assign a default method to a class like this:

public class EscMenu
{
    public static void EscMenu()
    {
        //do something
    }

   public static void SomeOtherMethod()
   {
       //do something else
   }
}

So when I call EscMenu.SomeOtherMethod(); in another class in the same solution, it does "do something else", but I cannot call EscMenu();.

How can I do that?

Thanks!

EDIT:

Okay, Im gonna try to explain this in a better way:

I just want the class EscMenu to do something when I call it from another (external) class like this: EscMenu();. Of course I could easily create a method default() in EscMenu and call EscMenu.default(); externally. But I would really like to just call EscMenu();

If that just isn't possible or I continue to fail in explaining myself, then just don't mind ;-)

Thanks again!

like image 532
janw Avatar asked Jun 02 '26 16:06

janw


1 Answers

No, you can't give a method the same name as its containing type - and you really don't want to confuse the name of a type with the name of a method anyway. Why introduce the ambiguity?

If you could give some example where you want to write code in some way other than what's already available, we may be able to help you more. (For example, it may be that extension methods could help.)

like image 192
Jon Skeet Avatar answered Jun 05 '26 06:06

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!