I have an abstract class with a protected variable
abstract class Beverage
{
protected string description;
}
I can’t access it from a subclass. Intellisense doesn’t show it accessible. Why is that so?
class Espresso:Beverage
{
//this.description ??
}
Short answer:
descriptionis a special type of variable called a “field“. You may wish to read up on fields on MSDN.Long answer: You must access the protected field in a constructor, method, property, etc. of the subclass.
Inside a
classdeclaration, you declare the members of the class. These may be fields, properties, methods, etc. These are not imperative statements to be executed. Unlike code inside a method, they simply tell the compiler what the members of the class are.Inside certain class members, such as constructors, methods, and properties, is where you put your imperative code. Here is an example: