Does the order of readonly variable declarations guarantee the order in which the values are set?
Posted
by Jason Down
on Stack Overflow
See other posts from Stack Overflow
or by Jason Down
Published on 2010-04-29T17:26:47Z
Indexed on
2010/04/29
17:37 UTC
Read the original article
Hit count: 320
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).
© Stack Overflow or respective owner