Run it here: http://rextester.com/ZMLMB2576
public interface IClass {
int number {get;}
}
public abstract class BaseClass : IClass {
public BaseClass(int n){
number = n+100;
}
public int number { get;set;}
}
public class DerivedClass : BaseClass {
public DerivedClass(int n) : base(n) {
number = n;
}
public int number { get;set;}
}
public class Program
{
public static void Main(string[] args)
{
var foobar = new DerivedClass(1);
Console.WriteLine(GetNumber(foobar)); // 101
}
public static int GetNumber(IClass foo){
return foo.number;
}
}
Why does the function GetNumber
not use the most derived class and instead only treats the passed object as BaseClass ?
If it was treating the passed object (foo) as DerivedClass then I suspect the base constructor to run first and then DerivedClass constructor, overriding 101 with 1