Does the order of readonly variable declarations guarantee the order in which the values are set?
- by Jason Down
Say I were to have a few readonly variables for filepaths, would I be able to guarantee the order in which the values are assigned based on the order of declaration?
e.g.
readonly string basepath = @"my\base\directory\location";
readonly string subpath1 = basepath + @"\abc\def";
readonly string subpath2 = basepath + @"\ghi\klm";
Is this a safe approach or is it possible that basepath may still be the default value for a string at the time subpath1 and subpath2 make a reference to the string?
I realize I could probably guarantee the order by assigning the values in a constructor instead of at the time of declaration. However, I believe this approach wouldn't be possible if I needed to declare the variables inside of a static class (e.g. Program.cs for a console application, which has a static void Main() procedure instead of a constructor).