Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In C# language,how many ways is chosed to store static variables, or something like static vars in the whole (.sln ) file?

And which one do you prefer?
Posted

By preference, I don't.
C# is a object oriented language, and everything is class based. It has no concept of "global" variables, which is what you are describing. However, there are occasions when a single application-wide variable is a good idea - the connection string to a database for example, could be read from a config file once, and stored in a static variable. Cases like this are very, very rare though - or there is something very wrong with your design.

When that happens, I generally construct a static class, and use that to contain the static variable, along with any non-class related methods (which again are as rare as possible).
 
Share this answer
 
You should have searched google for it. Following should help you.

Static Variables[^]
 
Share this answer
 

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