Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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]
Comments
- Anonymous
March 16, 2004
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. - Anonymous
March 17, 2004
The comment has been removed - Anonymous
March 17, 2004
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() - Anonymous
March 18, 2004
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. - Anonymous
March 19, 2004
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. - Anonymous
March 21, 2004
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? - Anonymous
March 21, 2004
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();
}
} - Anonymous
March 21, 2004
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. - Anonymous
April 20, 2004
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. - Anonymous
April 27, 2004
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 ? - Anonymous
July 17, 2004
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 - Anonymous
December 27, 2004
[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/] - Anonymous
January 18, 2016
<a href="www.tutorial4us.com/.../java-static-and-non-static-method.php">Difference between Static and non-static method in Java</a> Thanks for this post