Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
i just sending after i was done the a code ,

what is advantage of static variable.


we can call instead of creating the object only,or anyother ?


regards
James
Posted
Comments
Ali_100 31-May-12 1:29am    
I want to ask that ,is it necessary that to use static variable in class we must use static constructor to initialize?

Usually we don't call a variable. However, roughly speaking, yes, actually we can access a (public) static variable without creating an instance of its class.
static members should contain class attributes, that is data shared by all the instances of the class (none is all when no object is created).
 
Share this answer
 
In addition of CPalini's answer, C# defines seven categories of variables, among other things, static variables.
Have a look here:
Variable categories (C#)[^]
Static variables (C#)[^]

But i think, you want to know about static modifier (keyword). If yes, see here:
static (C# Reference)[^]

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
 
Share this answer
 
v2
Static variables are also improving performance as they are getting stores into a separate heap, a separate heap from Garbage Collector heap, which is referring as "high frequency heap". Refer Static Keyword Demystified[^].

Also variables can be defined without taking additional memory.

Static variables resolved good designing changeless like Singleton implementation.

Just Google it you will get lot more information about it.

Thanks
Rushi
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 30-May-12 16:00pm    
Poor lie. Why would you answer on things you have no idea about?!
Static variables cannot be "on top of heap", they have nothing to do with heap!
All variables always use some memory. How else?!
They are dangerous in many aspects. Their use is justified only where they are absolutely needed. Yes, like singleton, but the well-designed singleton does not use only static members.

I voted 1. More importantly, make sure you are very well familiar with the matter: confusing the inquirer make cost her/him too much of lost time, so not giving any advice is much better then giving a wrong answer.

--SA
Joshi, Rushikesh 30-May-12 16:54pm    
Thanks for updating me.
Joshi, Rushikesh 30-May-12 17:01pm    
SA, my wordings were confusing but my intention was right. I corrected my Answer with additional changes.

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