I'm sure this is a duplicate but I can't find an answer. I'm new to C# and having trouble understanding why this is happening:
I have one class that inherits from another. I want the subclass to override a public field on the superclass, in this case the public field a
. But it doesn't seem to work:
class Foo
{
public string a = "x";
public string A()
{
return a;
}
}
class Bar : Foo
{
public string a = "y";
}
...
new Foo().A() // Returns "x"
new Bar().A() // Returns "x" also...why?
I would expect the last line to return "y"
but instead it returns "x"
, ignoring the fact that I've overridden the value of the a
field. Why isn't it working? And what is the standard way of getting the behavior I want?
I'm entering this code into the csharp
REPL, if that matters.