简体   繁体   English

C#不能声明具有相同参数的静态和非静态方法?

[英]c# cannot declare static and non static methods with same parameters?

If I try to declare static and non-static methods with the same parameters compiler returns an error: type 'Test' already defines a member called 'Load' with the same parameter types. 如果我尝试使用相同的参数声明静态和非静态方法,则编译器将返回错误:类型“ Test”已经定义了具有相同参数类型的成员“ Load”。

    class Test
    {
        int i = 0;

        public int I
        {
            get { return i; }
            set { i = value; }
        }

        public bool Load(int newValue)
        {
            i = newValue;
            return true;
        }

        public static Test Load(int newValue)
        {
            Test t = new Test();
            t.I = newValue;
            return t;
        }

As far as I know these two methods can not be mixed, non static method is called on object whereas static method is called on class, so why does compiler not allow something like this and is there a way to do something similar? 据我所知,这两种方法不能混合使用,在对象上调用非静态方法,而在类上调用静态方法,那么为什么编译器不允许这样的事情,并且有办法做类似的事情?

If your Test class had a method like this: 如果您的Test类具有这样的方法:

public void CallLoad()
{
    Load(5);
}

the compiler would not know which Load() to use. 编译器将不知道要使用哪个Load()。 Calling a static method without the class name is entirely allowed for class members. 类成员完全允许调用没有类名称的静态方法。

As for how to do something similar, I guess your best bet is to give the methods similar but different names, such as renaming the static method to LoadTest() or LoadItem() . 至于如何做类似的事情,我猜最好的办法是给这些方法提供相似但不同的名称,例如将static方法重命名为LoadTest()LoadItem()

Inside the class itself, you call both instance methods and static methods without an instance or the class name, thus making the two undistinguishable if the names and parameters are the same: 在类本身内部,您可以调用实例方法和静态方法,而无需实例或类名,因此,如果名称和参数相同,则这两种方法就无法区分:

class Test
{
    public void Foo()
    {
        Load(0); // Are you trying to call the static or the instance method?
    }

    // ...
}

The signature of a method is the combination of name and parameters (number and types). 方法的签名是名称和参数(数字和类型)的组合。

In your case, your 2 methods have the same identical signature. 在您的情况下,您的2个方法具有相同的签名。 The fact that one is static and other one is not makes no difference in accepting them as valid methods for the class. 一个是静态的而另一个是静态的这一事实在接受它们作为该类的有效方法方面没有什么区别。

I don't think so. 我不这么认为。 if a non static method in this class calls Load(intValue). 如果此类中的非静态方法调用Load(intValue)。 which method will be called? 将调用哪种方法?

Both methods have the same name, defined in the same class (scope) and with the same signature. 两种方法具有相同的名称,在相同的类(作用域)中定义且具有相同的签名。 C# does not allow this. C#不允许这样做。

The problem is not related with writing this or the classname . 问题与编写thisclassname无关。 C# specs allow you to call static methods using object instances: C#规范允许您使用对象实例调用静态方法:

AClass objectA = new AClass();
objectA.CallStaticMethod();

This code is valid so the compiler never has a way to know if you're calling a static or an instance method. 该代码有效,因此编译器永远无法知道您是在调用静态方法还是实例方法。

In C# a method cannot be overloaded by return type. 在C#中,返回类型不能重载方法。 It must at least have a different set of parameters, regardless if the method is static or not. 无论该方法是否静态,它至少必须具有一组不同的参数。

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

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