C# 具有相同名称的静态和实例方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/160118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static and Instance methods with the same name?
提问by Dan Herbert
I have a class with both a static and a non-static interface in C#. Is it possible to have a static and a non-static method in a class with the same name and signature?
我在 C# 中有一个包含静态和非静态接口的类。是否可以在具有相同名称和签名的类中具有静态和非静态方法?
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 wrong 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 like this that can be applied generically to any situation?
如果这是不可能的,有没有一种好方法来实现这样的东西,可以普遍应用于任何情况?
EDIT
From the responses I've received, it's clear that there is no way to do this. I'm going with a different naming system to work around this problem.
编辑
从我收到的回复来看,很明显没有办法做到这一点。我将使用不同的命名系统来解决这个问题。
采纳答案by ckramer
No you can't. The reason for the limitation is that static methods can also be called from non-static contexts without needing to prepend the class name (so MyStaticMethod() instead of MyClass.MyStaticMethod()). The compiler can't tell which you're looking for if you have both.
不,你不能。限制的原因是静态方法也可以从非静态上下文调用,而无需预先添加类名(因此 MyStaticMethod() 而不是 MyClass.MyStaticMethod())。如果您同时拥有两者,编译器将无法判断您正在寻找哪一个。
You can have static and non-static methods with the same name, but different parameters following the same rules as method overloading, they just can't have exactly the same signature.
您可以拥有同名的静态方法和非静态方法,但不同的参数遵循与方法重载相同的规则,只是不能具有完全相同的签名。
回答by Matt Hamilton
You can call static methods from instance methods without having to specify the type name:
您可以从实例方法调用静态方法,而无需指定类型名称:
class Foo
{
static void Bar()
{
}
void Fizz()
{
Bar();
}
}
... so it makes sense that you wouldn't be allowed to have a static method and an instance method with the same signature.
...因此,不允许您拥有具有相同签名的静态方法和实例方法是有道理的。
What are you trying to accomplish? It's hard to suggest a workaround without knowing specifics. I'd just rename one of the methods.
你想达到什么目的?在不知道具体情况的情况下很难提出解决方法。我只是重命名其中一种方法。
回答by Franci Penov
You can have static and instance method with the same name, as long as their declaration differs in the number or type of parameters. It's the same rule on how you can have two instance methods with the same name in a class.
你可以有同名的静态方法和实例方法,只要它们的声明在参数的数量或类型上不同。关于如何在一个类中拥有两个具有相同名称的实例方法,这是相同的规则。
Though technically, in the case of static vs. instance method, they already differ by the presence of the implicit this parameter in the instance method, that difference is not enough for the compiler to determine which of the two you want to call.
尽管从技术上讲,在静态方法与实例方法的情况下,它们已经因实例方法中隐式 this 参数的存在而有所不同,但这种差异不足以让编译器确定您要调用两者中的哪一个。
Update: I made a mistake. Return values are not enough to have different signature.
更新:我犯了一个错误。返回值不足以具有不同的签名。
回答by andasa
Actually, there kind of is a way to accomplish this by explicitly implementing an interface. It is not a perfect solution but it can work in some cases.
实际上,有一种方法可以通过显式实现接口来实现这一点。这不是一个完美的解决方案,但它可以在某些情况下工作。
interface IFoo
{
void Bar();
}
class Foo : IFoo
{
static void Bar()
{
}
void IFoo.Bar()
{
Bar();
}
}
I sometimes run into this situation when I make wrapper classes for P/Invoke calls.
当我为 P/Invoke 调用制作包装类时,有时会遇到这种情况。
回答by Adrian
OK. The root of this problem is that C# should not let you call a static method from an instance method without specifying the type name.
好的。这个问题的根源在于 C# 不应该让你从实例方法调用静态方法而不指定类型名称。
Other full OO languages (like Smalltalk) don't allow this and also its just confusion to people who understand objects. The seperation between instance side and class (or static) side is very important and having a language that promotes confusion in those details is........not a good idea....but typical of the type stuff we expect from MS.
其他完整的 OO 语言(如 Smalltalk)不允许这样做,这对理解对象的人来说也是一种困惑。实例端和类(或静态)端之间的分离非常重要,使用一种语言来促进这些细节的混乱......不是一个好主意......但是我们期望的典型类型的东西来自 MS。
Adrian
阿德里安
回答by Nick Sotiros
C# is not well designed when it comes to this...
C# 在这方面设计得并不好......
While it is true that you could want the global or non-global, it should pick one by default, and if you want the other then you simply qualify 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
}
}