I am trying to understand the way inheritance works in C#. Basically, I have a base class Base that has a simple function sayIt. The function makes use of a property "i" that is redefined in subclasses. Here, it is redefined as 1 from 0. When I run this program, I get "0" as output rather than "1" which is what I expected (because in python I would get 1). Can anyone explain why, and more importantly, whether this is a pattern that is supported in C#?
class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
Console.WriteLine(d.sayIt());
Console.ReadLine();
}
}
class Base
{
int _i = 0;
public int i
{
get { return _i; }
}
public String sayIt()
{
return Convert.ToString(this.i);
}
}
class Derived : Base
{
int _i = 1;
public new int i
{
get { return _i; }
}
}