I have a class with a static string variable that has somewhat complicated initialization (I cannot just set it equal to the quoted string, for example "whatever"). I need to run a few lines of code in order to actually create a value. Once it is installed, its value will not change. It is currently configured as a property that is simply set on the first call get.
class MyClass
{
private static string _myString = "";
public static string MyString
{
get
{
if(_myString == "")
{
MyObject obj = new MyObject();
obj.someSetupHere();
_myString = obj.ToString();
}
return _myString;
}
}
}
My question is: is there a better way to do this? I would prefer that the value be set when all other variables are set, and not on the first "get" value. Should I use here Lazy<T>? I would really like:
private static string _myString =
{
MyObject obj = new MyObject();
obj.someSetupHere();
_myString = obj.ToString();
}
, , , , , , , .