Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class A
             {
                    private double x1 ,x2;
	
                    public static void Main()
                    {
                        x1  =  100;
                        Console.WriteLine (x1);
                    }
                }


What I have tried:

these are questions for my revision notes, I purely need answers to my questions this is not code I am working on for anything!
Posted
Updated 20-Jan-18 5:19am
v3

Main cannot access x1 or x2 because they are instance variables: they only exist on instances of a class. Main is a static method, so it's not associated with an instance, so it can't access x1 or x2: it would only be able to access these two variables if they were marked static as well.

And why does Main have to be marked as static? That's just how C# works: it's defined not to create a class instance for the entry point, but it's defined to run the static method called Main.
 
Share this answer
 
v2
The static keyword is a special thing in C#, it says that "regardless of how many instances of this class there are, there is only ever one of these"
It's like a physical constant in maths: there is one and only one value of Pi - but you can use it in as many equations and calculations as you like. Pi is a static value in the class of calculations.

Instances are different: think about cars. This is my car, that it your car. Over there is that car, this car is blue, that car is red, my car is black. We know that when we discuss "my car" we are talking about a specific car: and we do not expect to put your mobile in the glove box of "your car" and then retrieve it from the glove box of "my car". We are aware that each car is a separate instance of a vehicle in the class of Cars. We we also all know that you can't ask "what colour is a car?" because not all cars are the same colour. Colour is an attribute of a specific instance of a car, so to access it we need to specify exactly which car we are talking about. But ... all cars have four wheels (or they wouldn't be a car) so "how many wheels has a car?" is a valid question.
Colour is an instance question; WheelsCount is a static question
Classes in C# work the same: instance data requires an instance of the class, static data doesn't.

When the system runs the app, it needs to start executing somewhere - so it looks for the static Main method. If it wasn't static there could be dozens of them, and it would need an instance in order to run, but you can't create an instance before the program runs!

So Main is static - and as a result it doesn't have an instance associated with it - so it can't access instance variables because it doesn't know if it's supposed to use "this instance" or "that instance".

You can let Main access it in two ways:
1) Make x1 static:
C#
class A
    {
    private static double x1;
    private double x2;
    
    public static void Main()
        {
        x1 = 100;
        Console.WriteLine (x1);
        }
    }

Or
2) Create an instance of your class:
C#
class A
    {
    private double x1, x2;
    
    public static void Main()
        {
        A a = new (A);
        A.x1 = 100;
        Console.WriteLine (A.x1);
        }
    }

Make sense?
 
Share this answer
 
Comments
Member 13634122 20-Jan-18 11:12am    
That has completely cleared it up for me, great explanation, thanks a lot!
OriginalGriff 20-Jan-18 11:22am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900