Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir;

What is the difference between public static and public variables in c# by example?

Thanks in advance
Dalia
Posted
Updated 27-Apr-12 22:02pm
v2
Comments
Mohibur Rashid 28-Apr-12 3:29am    
are you sure you are talking about C++ but not java? if you are right I will have to restudy C++

lets say I have a class
C#
class A
{
 public int x;
 static int y;
}

now the differences:
to use public variable i have to have objects created so
C#
A a = new A();
int num = a.x;

on the other hand static variables are at class level so I can be used as
C#
int num = A.y;

or
C#
A a = new A();
int num = a.y;

ALso the static variable retain the values for all objects of the class. to illustrate lets see this:
C#
A a1 = new A();
A a2 = new A();

a1.x = 3;
a2.x = 5;

Console.Write(a1.x);    //print 3
Console.Write(a2.x);    //print 5

a1.y = 3;
Console.Write(a1.y);    //print 3
Console.Write(a2.y);    //print 3

a2.y = 5;
Console.Write(a1.y);    //print 5
Console.Write(a2.y);    //print 5

On the closing note, we should never use public member variables, we should always use private variables encapsulated inside properties or methods. on the other hand static variables might be required in a class.

Note: code here is for demonstration purpose, might contain come errors as I have not compiled it. ALso this is not the complete explanation. it is just the basic explanation. i suggest reading more about this to understand these things fully.
 
Share this answer
 
v2
Comments
VJ Reddy 28-Apr-12 4:55am    
Good example. 5!
 
Share this answer
 
Comments
VJ Reddy 28-Apr-12 4:56am    
Good reference. 5!
It's difficult to work out what you are trying to learn, because they aren't the same thing - and at least one of them is not recommended anyway.

Starting at the beginning:
There are two types of variable: instance and static. If you do not declare a variable as static, then it is always instance. The difference is that an instance variable is a part of a particular instance of a class while a static variable is common to all instances of the class, and does not require a instance be generated in order to be accessed.

For example, take cars - real ones.
How many wheels has a car? 4 - or it wouldn't be a car. So WheelsCount would be a static property of all cars - you can ask Car.WheelsCount and expect it to work every time.
What colour is a car? It isn't - cars come in a variety of colours, so you have to pecific which car you are talking about - or which instance of a Car you want to know the colour of: "What colour is your car?" "What colour is my car?" "What colour is that car?". Colour is an instance property of a Car.

This static / instance difference is applicable to variables, properties and methods in C#

Public does not have any relation to the static / instance difference - it just defines what classes can access the item: a private item can only be accessed by the class it is contained within, protected by that class and those derived from it, internal from all classes in the same assembly, and public by any class anywhere.
If we go back to the cars analogy, a private car would be one where only you have the key, protected where you and your wife have keys, and public would be when you park it outside the house and leave the keys in the ignition!

You can have public static variables, or private static variables - the two modifiers are not related.


[edit]Fix to internal - corrected from namespace to assembly. Thanks VJ Reddy! - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Rahul Rajat Singh 28-Apr-12 4:32am    
Awesome explanation, loved the analogy.
VJ Reddy 28-Apr-12 4:53am    
Nice explanation. 5!. I think internal is for within same assembly
OriginalGriff 28-Apr-12 5:02am    
:doh:
Caffeine, I need more caffeine...
Thanks - I corrected and credited.
VJ Reddy 28-Apr-12 5:07am    
Thank you very much, OriginalGriff.

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