Why can't I have static and instance methods with the same name?

Why can't I have static and instance methods with the same name?

  • Comments 12

Q: Why can't I have static and instance methods with the same name?

class Test
{
    static void Process();
    void Process();
    void AmbiguousCaller() { Process(); }
}

there's no ambiguity, since the first can only be called through the type name, and the second can only be called through an instance.

A:

It is true that there would be no ambiguity between the two functions as far as a compiler is concerned. There would, however, be a considerable potential for confusion on the part of the user. It would be tough to find the right method in documentation, and once you did, hard to be sure that you are calling the right version (ie you could accidentally call the static version when you wanted the instance version).

We therefore prohibit this case.

[Author: Eric Gunnerson]

Leave a Comment
  • Please add 2 and 3 and type the answer here:
  • Post
  • There is still ambiguity. Consider:

    class Test
    {
    static void Process();
    void Process();
    void AmbiguousCaller() { Process(); }
    }

    Does AmbiguousCaller() call static Test.Process() or does it call this.Process()?

    Aside from this I believe there would also be ambiguity from MC++ where class statics can be invoked through an instance pointer.
  • I disagree with your belief that a user would get confused between the two function calls (at least not in C#).

    To call your static function you would have to invoke it as Test.Process();

    To call your instance of the funtion you would have to us an instance:

    new Test().Process();
    or
    Test obj = new Test();
    obj.Process();

    I can't see how you could get confused here. I would also assume that the static function would perform the same functionality as the instance version of it, but the static probably handles the creation and destuction of the object. IE I would expect that the following would do the same:

    Guid.NewGuid();
    New Guid().NewGuid();

    Personally I think this is a time saver for end users of the component.
  • Q:

    Does AmbiguousCaller() call static Test.Process() or does it call this.Process()?

    A:

    It calls this.Process(). You must use the class name to call a static function. So the only way to call the static function is to call Test.Process()
  • Well, in the current language specification you don't *have* to use the class name to call a static method - so under the current specification, it *would* be ambiguous.
  • Excerpt from Microsoft Programming Languages (http://msdn.microsoft.com/vstudio/productinfo/whitepapers/default.aspx)

    "Shared members may be accessed in Visual Basic .NET through both the class name and an instance variable of the type to which they belong."

    That makes me wonder if in C# having to call a static member through the class name and not allowing it to be called by an instance variable is a C# imposition rather than a CLR one.
  • Um, ok perhaps I am doing something wrong here. I just tried calling a static method without the class name in C# and it wouldn't compile.

    I don't use VB so I am not sure how that is working. I am asuming that the compiler converts the instance call to the class call.

    So if this is the current specification, why isn't C# or the CLR following it?
  • Could you give a sample of it not compiling? You have to use a class name if it's a method of a *different* class, but if it's a method in the same class, it's okay. Here's an example:

    class Test
    {
    static void Main()
    {
    new Test().InstanceMethod();
    }

    static void StaticMethod()
    {
    }

    void InstanceMethod()
    {
    StaticMethod();
    }
    }
  • There is actaully a very simple reason why it is illegal. In C# it is legal to have a local variable that is the same as a class name. This means that if an instance member was to be named the same as static member there would be an ambiguity when a local variable had the same name as the class it represents.

    So consider the following legal C# code:

    public class TestClass
    {
    public TestClass(){}

    public static void MyStaticMethod()
    {}

    public void MyInstanceMethod()
    {}
    }

    public class StartUp
    {
    public static Int32 Main(String[] args)
    {
    TestClass TestClass = new TestClass();
    TestClass.MyInstanceMethod();
    TestClass.MyStaticMethod();
    return 0;
    }
    }

    If MyInstanceMethod & MyStaticMethod names were change to be the same there would be an ambiguity.
  • Why it is legal to have have a local variable that is the same as a class name in C#?? why not in VB.NET and other languages? If we don't allow name of a local variable that is the same as a class name then this confusion won't happen.

  • From what I have heard VB.NET allows calling of static methods from a non-static context.
    Wouldnt there be an ambiguity in that case when you use this test class to derive a descendant class in vb.net ?



  • my web:
    http://www.sj55.com/pic_sort http://www.zw88.com/paopaotang.htm http://www.zw88.com/sj.htm http://www.zw88.com/sm/
    http://www.zw88.com/sms/ http://www.zw88.com/zw.htm http://www.resou.com/8888.htm http://www.sj55.com/pic/pic_1130.htm
  • [http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]
Page 1 of 1 (12 items)