Click here to Skip to main content
15,877,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If we are declaring any class as static class and also member variable in that class as static. What will exactly happen? Is it good to do so?

C#
Public static class DataManager
{
 public static List<int> productIDs=new List<int>();
// Some code....
//
}
Posted
Updated 4-Dec-14 18:55pm
v3
Comments
PIEBALDconsult 4-Dec-14 23:47pm    
You would have to. Have you tried it?

Yes your class is correct what exactly you want to know.

For Static Class it’s not possible to create an Object. In Static Class only Static members are allowed which means in a static Class it’s not possible to write non-static method.

Check my article related to Static Class and Method
Basic C# OOP Concept[^]
 
Share this answer
 
"Is it good to do so?"
imho, it is "best" when you have made a conscious choice to use a static implementation method because it is an "organic" consequence of what your code does, how it is used, and your intent, and application design.

It may be helpful if you ask yourself this question every time you use the 'static keyword:

"Do I intend/need/want for this (whatever) to be the one-and-only-one of this ? Depending on your culture, you might also think of that part of the (typical "western") marriage vows that says "forsaking all others ... until death do us part." :)

Keep in mind that public classes that can be instantiated with 'new can have static variables, methods. In fact .NET itself is full of examples of this: i.e., the 'String object.

Among important uses of 'static in .NET are:

1. constructing utility classes that provide libraries of static methods (or extension methods).

2. in creating 'singleton Classes

3. in implementing the 'Factory Pattern

The 'static access modifier in C# is "abused," imho, when it is used as a way to create the equivalent of global variables (as found in JavaScript, and Visual Basic) in order to avoid more "formal" techniques for passing objects/data/state between objects/classes/controls.
 
Share this answer
 
static members variable in static class is possible...

we can't create instance of static class ,
eg.


public static class sample

{
public static fun_sample()

{

}
}

we can access this method directly by sample.fun_sample();
 
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