简体   繁体   English

静态和实例方法同名?

[英]Static and Instance methods with the same name?

I have a class with both a static and a non-static interface in C#. 我在C#中有一个同时具有静态和非静态接口的类。 Is it possible to have a static and a 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 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. 我要使用其他命名系统来解决此问题。

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()). 进行限制的原因是,也可以从非静态上下文中调用静态方法,而无需添加类名(因此,可以使用MyStaticMethod()代替MyClass.MyStaticMethod())。 The compiler can't tell which you're looking for if you have both. 如果两者兼有,编译器将无法确定您要查找的内容。

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. 您可以使用具有相同名称的静态方法和非静态方法,但是不同的参数遵循与方法重载相同的规则,它们不能具有完全相同的签名。

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调用创建包装类时,有时会遇到这种情况。

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. 我只是重命名其中一种方法。

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
   }
}

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. 返回值不足以具有不同的签名。

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. 其他完整的OO语言(例如Smalltalk)不允许这样做,并且这对于理解对象的人来说也很混乱。 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. 实例端与类端(或静态端)之间的分隔非常重要,使用一种语言来促进这些细节上的混淆是........不是一个好主意。来自MS。

Adrian 阿德里安

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM