Property overloading - static vs instance
I'd like to understand the reasoning why the following is not allowed by csharp:
class Foo
{
public Bar Property
{
}
public static Bar Property
{
}
}
Obviously these two properties do not have the same signature. Can someone explain?
- Ray
The reason is that some languages (C++ and VB.NET come to mind) allow you to access static properties via instances as well as the class name. Let's say you have a static property called Whatever :
public class Foo2 {
public static Bar2 Whatever {
get {
return new Bar2();
}
}
}
In VB.NET you could say:
Sub Main()
Dim f As New Foo2
Dim b1 As Bar2
Dim b2 As Bar2
b1 = Foo2.Whatever
b2 = f.Whatever
End Sub
Best practices dictate (in both C++ and VB.NET) to access static property using the class name.
It is a .NET Framework limitation that property (and method) names must be unique across both static and instance so that all .NET languages can access the properties (and methods).