2

I'm currently looking at some old code and I've come across a class that is using a private static property which is created with a default value and never modified; something like this -

public class Foo { private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnection"].ToString(); public Bar GetBar(int barId) { // get bar using "ConnectionString" above } } 

So my question is - Is there any benefit to ConnectionString being static? i.e. Is ConfigurationManager.ConnectionStrings["SqlConnection"].ToString(); run every time new Foo() is run thus making the static value of the property redundant as it is overwritten every time the class is initialised?

3
  • 3
    run every time new Foo() is run No. It runs once. If you removed the word static it would act basically the way you are describing (i.e. everytime Foo was created - although it wouldn't be overwriting a singleton like static does). Commented Feb 27, 2018 at 11:15
  • It is the purpose of static keyword. If you want it to run every time you don't add the static keyword. Commented Feb 27, 2018 at 11:16
  • No, static means once for the entire application, not once per instance. The docs are pretty clear about that: "A static constructor is only called one time" (learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…) Commented Feb 27, 2018 at 11:27

2 Answers 2

7

Static fields are initialized once when the first object of that class is instantiated, not every time an object is created. That makes them relatively efficient.

However, there is a downside to this, and that is that the instance-level constructors are embellished with a state machine which determines whether the one-time initialization has been completed or not.

When the first object of the class is being created, the static constructor will be invoked before any other code executes on an instance level. For subsequent instantiations, this step will be skipped, because the class has already been initialized.

This additional code, which is generated during the compilation, makes every instance-level constructor a tiny bit slower than it would otherwise be without the static members.

Sign up to request clarification or add additional context in comments.

4 Comments

Horvart : if the static property is marked [ThreadStatic] then I think this behavior is specific to each thread and not as a whole in app domain
If the OP doesn't understand static then I don't think bringing ThreadStatic into the discussion is going to be helpful @RahulAgarwal .
When [ThreadStatic] is included, then this behavior is a bit different, in sense that it is repeated on a per-thread basis. But the same principle applies.
Thanks for this. I understood that statics retained their values across instances however seeing ConnectionString = .... just made me think that it would happen every time.
4

No, static members are initialized only once before the class is referenced for the first time in your program and it remains in memory for the lifetime of the application domain.

But in this case the field is redundant because the ConfigurationManager caches this value anyway, so doesn't read it from the configuration file everytime you access it.

2 Comments

What if this static variable is initialized inside the constructor of the class? (its value is assigned in the constructor).
@FryingPan then every instance overwrites the old value. So one use case for this is to count the instances of this class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.