Context of my question:
- I am reading C#.
- Trying to understand Static keyword.
- I understand that when Static is applied to a member of a class, it can only be modified by the class and not the class object references.
I will take an example here.
public class BluePrint
{
public static string BluePrintCreatorName;
}
If I need to know the BluePrintCreator's Name, I can call
BluePrint.BluePrintCreatorName;
But if a house that is created from the blueprint wants to know the BluePrintCreator's Name, it does not have access.
BluePrint NewHouse = new BluePrint();
NewHouse.BluePrintCreatorName; //This is not accessible
Compiler error says:
Member 'AccessModifier.BluePrint.BluePrintCreatorName' cannot be accessed
with an instance reference; qualify it with a type name instead
I understand this is the way it works. But I want to know the basic concept to why Static keyword was required at all?
What will go wrong if a class object reference would have access to the static member?
BluePrint.BluePrintCreatorName
instead ofNewHouse.BluePrintCreatorName
. Simple fix.