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?
run every time new Foo() is runNo. It runs once. If you removed the wordstaticit would act basically the way you are describing (i.e. everytimeFoowas created - although it wouldn't be overwriting a singleton likestaticdoes).