There is a behavior in C# I really do not understand:
class Animal {
public string species = "animal";
public void Introduce() {
Console.WriteLine(species);
}
}
class Cat: Animal {
new public string species = "cat";
// public void Introduce() {Console.WriteLine(species);}
}
class Program
{
static void Main(string[] args)
{
var cat = new Cat();
cat.Introduce();
}
}
This piece of code, when executed, outputs >>> animal
.
For me, since Cat inherits Animal, calling cat.Introduce should call Animal.Introduce "in the scope of the cat instance". ie I don't understand why the program picks the species
field of Animal and not the one of Cat...
I know I could use workarounds, but I believe I'm missing some point about c# design, can someone explain this behavior?
Thanks