4

Why C# allows initializing static class variables in non-static contructor? The static variables should only be allowed to be initialized on static constructors. Any ideas?

 public class customer
{
    public string Name;

    public customer()
    {
        Name = "C1";
        Console.WriteLine("creating customer " + Name);
    }

}

class Program
{
    public static customer cust;

    public Program()
    {
        cust = new customer(); //Why is this allowed i.e. initialize a static variable in non-static constructor?
    }

    static void Main(string[] args)
    {
        Program program = new Program();
        program = new Program();

        Console.Read();
    }
}
2
  • 7
    Why should it not allow this? What if you want to initialize a static variable the first time a class is instantiated? Or, perhaps increment a count each time an object is new'ed up. Commented Jul 31, 2013 at 17:08
  • 2
    You can update it from any method, there's nothing special about the constructor. Did you forget to declare it readonly? Commented Jul 31, 2013 at 17:08

4 Answers 4

13

Don't look at it as initializing, look at it as setting.

If you would only like it to be initialized via a static constructor or at declaration, add the readonly keyword.

E.g.

public readonly static customer cust;

//Allowed
static Program()
{
    cust = new customer(); 
}

//Not Allowed
public Program()
{
    cust = new customer();
}
Sign up to request clarification or add additional context in comments.

2 Comments

so static keyword alone is not enough to force single initialization? This behaviour is different from C++.
@naveed c# is not c++.
4

The short answer is there is no reason why not to allow this. Static variables can be reached anywhere from within the class (and outside, if they're public) and the constructor is no exception. This includes changing their value, or initializing them to a new value.

There are, in fact, several patterns that can take advantage of this behavior. For example, initializing a static object the first time a class is instantiated (perhaps for caching properties that are expensive to initialize but don't change in the future). Another use might be incrementing a counter to keep track of how many of these objects are alive.

With that said, you'd want to be aware of static objects in a class before initializing, and check to see if they're null before overwriting their values.

Comments

2

You can access and modify a static variable from any nonstatic function, it will just overwrite the value each time it is called. The opposite is not true, though - static functions can't access nonstatic variables.

Comments

0

It just means that the static variable is reset every time a new object is initialized. A bit weird, but the compiler allows it.

2 Comments

yep, true. Doesn't make sense. If I declare and initialize static variable at the same time, then it is initialized only once.
@naveed You're missing the point. You're the one doing the silly thing, not the compiler. Static constructor handles initializing static members, however, static simply means "accessible without an instance", not "assignable only once". If you want the field to be only assignable once, in the constructor, you can use the readonly keyword. Don't assume that since the keyword has the same name as in a different language, it has to be the same. static in C++ is similar in concept, but still different from static in C#. Also, wouldn't you have to declare as const static in C++ too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.