Console.WriteLine(i);
Console.WriteLine(base.i);
Result:
9
13
Edit: in response to feedback from Karthik and Muhamad:
Yes, I'm not on-target here, not thinking about the fact the method is static because it's the special 'Main method launch-pad for a Console App: you can
never use the keyword 'base to access an ancestor class' fields, or methods, in a static method whether that static method is
within a dynamic (non-static) class, or a static class !
And, of course, using static Classes you do not have inheritance
from other classes at all: all static classes inherit from 'Object.
There is a way in a static class to call a method of a dynamic class, and in that method, you can access the base class it inherits from. Let me illustrate in code that's not a Console app, but done in WinForms:
public class baseClass
{
protected int i = 13;
}
public class inheritsFromBaseClass : baseClass
{
private int i = 9;
private int b = 9;
public void showValues()
{
Console.WriteLine(i.ToString());
Console.WriteLine(base.i.ToString());
}
}
public static class xProgram
{
public static inheritsFromBaseClass childOfBase;
static xProgram()
{
childOfBase = new inheritsFromBaseClass();
}
}
If you invoke the static class 'xProgram, like this:
xProgram.childOfBase.showValues();
You create a
static instance of the dynamic class, 'inheritsFromBase named 'childOfBase, and when you invoke that instance's 'showValues method, that method can, and does, use the word 'base to access its ancestor class' value for 'i.
Summary:
1. you are in a special 'zone when writing a Console Application if you try and use inheritance
inside the static 'Main method.
2. there are ways to get access to a dynamic class' ancestor class from a static method (as shown in the code here). But, think about what's happened: the
compelling reason to use dynamic classes is the facility to create
multiple instances of them.
In this case there will be one, and only one, instance of the 'inheritsFromBaseClass encapsulated within the xProgram static class ! I suspect that's
not a good thing.
Suggestion: consider prototyping your experiments in OOP using
only dynamic classes.
Sorry if my response has "muddied the waters" here ! I have virtually no experience with Console applications.