Static and instances with the same name?

I have a class with a static and non-stationary interface in C #. Is it possible to have a static and non-static method in a class with the same name and signature?

I get a compiler error when I try to do this, but for some reason I thought there was a way to do this. Am I mistaken or is there no way to have both static and non-static methods in the same class?

If this is not possible, is there a good way to implement something similar that can be applied in the general case to any situation?

EDIT
From the answers I received, it is clear that there is no way to do this. I am going with a different naming system to get around this problem.

+46
c # oop
Oct 01 '08 at 22:48
source share
6 answers

No, you can’t. The reason for the limitation is that static methods can also be called from non-static contexts without the need to add a class name (so MyStaticMethod () instead of MyClass.MyStaticMethod ()). The compiler cannot say what you are looking for if you have both.

You may have static and non-static methods with the same name, but different parameters that meet the same rules as method overloads simply cannot have exactly the same signature.

+51
01 Oct '08 at 22:55
source share

This is actually a way to do this by explicitly implementing the interface. This is not an ideal solution, but in some cases it may work.

interface IFoo { void Bar(); } class Foo : IFoo { static void Bar() { } void IFoo.Bar() { Bar(); } } 

Sometimes I come across this situation when I create wrapper classes for P / Invoke calls.

+34
Feb 20 '12 at 17:50
source share

You can call static methods from instance methods without specifying a type name:

 class Foo { static void Bar() { } void Fizz() { Bar(); } } 

... therefore it makes sense that you will not be allowed to have a static method and an instance method with the same signature.

What are you trying to achieve? It is difficult to suggest a workaround without knowing the specifics. I would just rename one of the methods.

+11
Oct 01 '08 at 22:52
source share

C # is not well designed when it comes to this ...

Although it is true that you may want global or non-global, it should choose one by default, and if you want another, then you just get it more.

 class Logger { public static Logger instance; public static void Log(string message) { instance.Log(message); // currently the compiler thinks this is ambiguous, but really its not at all. Clearly we want the non-static method } public void Log(string message) { } public void DoStuff() { Log("doing instance stuff"); // this could be ambiguous, but in my opinion it should default to a call to this.Log() Logger.Log("doing global stuff"); // if you want the global qualify it explicitly } } 
+2
Apr 29 '17 at 15:26
source share

You can have a static and instance method with the same name if their declaration differs in the number or type of parameters. This is the same rule about how you can have two instance methods with the same name in the class.

Although technically, in the case of a static method against an instance, they already differ by the presence of an implicit parameter in the instance method, this difference is not enough for the compiler to determine which of the two you want to call.

Refresh . I made a mistake. The returned values ​​are not enough to have a different signature.

0
Oct 01 '08 at 22:52
source share

OK The root of this problem is that C # should not allow you to call a static method from an instance method without specifying a type name.

Other full OO languages ​​(e.g. Smalltalk) do not allow this, and also just confuse people who understand objects. The separation between the side of the instance and the class (or statics) is very important and having a language that encourages confusion in these details is ........ not a good idea ... but typical of the type that we expect from MS.

Adrian

-one
Jun 05 '13 at 23:47
source share



All Articles