I will show you some code, like in an interview question. Try to figure out what happened, then read on. The question is: what does the following code write to the console:
class Program
{
staticvoid Main(string[] args)
{
var c = new MyClass();
c.DoWork();
Console.ReadKey();
}
}
class BaseClass
{
public BaseClass()
{
DoWork();
}
publicvirtualvoid DoWork()
{
Console.WriteLine("Doing work in the base class");
}
}
class MyClass:BaseClass
{
private readonly string _myString = "I've set the string directly in the field";
public MyClass()
{
_myString = "I've set the string in the constructor of MyClass";
}
publicoverridevoid DoWork()
{
Console.WriteLine($"I am doing work in MyClass with my string: {_myString}");
}
}
Comments
Be the first to post a comment