
Hello,
In the following code, the property type of IntValue.Value is recognized to be a System.Object instead of a System.Int32. Therefore the statement in Program.Main() is considered faulty (because you attempt to assign an object to an int. However, it is correct because the property Value is redefined (using the "new" modifier) with the generic type T, which is specialized to Int32 in class IntValue.
public static class Program
{
public abstract class ValueBase
{
public abstract object GetValueObject();
public object Value { get { return GetValueObject(); } }
}
public abstract class ValueBase<T>: ValueBase
{
public abstract T GetValue();
public override sealed object GetValueObject();
{
return GetValue();
}
public new T Value { get { return GetValue(); } }
}
public class IntValue: ValueBase<int>
{
readonly int _value;
public IntValue(int value)
{
_value = value;
}
public override int GetValue()
{
return _value;
}
}
static IntValue _intValue = new IntValue(42);
static void Main()
{
int value = _intValue.Value;
}
}
(note: As i wrote this from my head without testing, there may be flaws. I hope its accurate enough for you to recognize the issue)
Thank you very much for taking a look at this. :)