According to this question it seems like you can do this for Methods. What I want to know is why it doesn’t work when I try it with properties.
public class Foo
{
public virtual object Value
{
get;
set;
}
}
public class Foo<T> : Foo
{
public override object Value
{
get
{
return base.Value;
}
set
{
base.Value = (T)value; //inject type-checking on sets
}
}
public new T Value
{
get { return (T)base.Value; }
set { base.Value = value; }
}
}
Error message from C# 4.0 RC1
Error 1 The type
‘ClassLibrary1.Foo’ already
contains a definition for ‘Value’
ClassLibrary1\Class1.cs 31 22
ClassLibrary1
You can’t have two properties using the same name. This is not allowed in C#. The only exception to this is indexers, since, in the case of an indexer, the signature changes by the index type.
You cannot make an overload for a method that only differs by the return type. A property with a single name is basically prohibited by the same rule, since it is internally a pair of methods with no argument for the get accessor.