Initializing a static field vs. returning a value in static property get?
Solution 1
In your first example, LoadSearchList() will be called each time the property is accessed.
In the second, LoadSearchList() will only be called once (but it will be called whether you use it or not since it is now a field rather than a property).
A better option might be:
private static IEnumerable<string> _searchWordList;
public static IEnumerable<string> SearchWordList
{
get
{
return _searchWordList ??
( _searchWordList = DataTools.LoadSearchList());
}
}
Or if you're using .NET 4.0 and want something thread-safe you can use Lazy<T>
, as Jon Skeet mentioned (I think the syntax should be correct, but don't hold me to it):
private static Lazy<IEnumerable<string>> _searchWordList =
new Lazy<IEnumerable<string>>(() => DataTools.LoadSearchList());
public static IEnumerable<string> SearchWordList
{
get { return _searchWordList.Value; }
}
Solution 2
In the first case the method will be called every time the property is accessed. When it's set as a field, it will be run exactly once - when the type it initialized - whether or not it's ever accessed.
If you want a lazily-initialized value, I'd recommend Lazy<T>
from .NET 4.
Solution 3
Yes, the property will call DataTools.LoadSearchList() every time access. The static field will only call the method once.
magnattic
Working mostly on the web. Passionate about technology and innovation.
Updated on October 26, 2020Comments
-
magnattic over 3 years
A) In the following code, will the method
DataTools.LoadSearchList()
only be called once, or every time the property is being accessed?public static IEnumerable<string> SearchWordList { get { return DataTools.LoadSearchList(); } }
B) Is there any difference to this?
public static IEnumerable<string> SearchWordList = DataTools.LoadSearchList();
-
Jon Skeet about 13 yearsNote that it might be called more than once, if there are multiple threads involved. Personally I'd prefer to use
Lazy<T>
from .NET 4, if that's an option. Then it's easy to adjust this sort of thing to make the second concurrent caller block, for example. -
Justin Niessner about 13 years@Jon Skeet - Updated with an example...hopefully I got the syntax right.
-
Jon Skeet about 13 yearsLooks good to me, although I think you could probably just do
new Lazy<IEnumerable<string>>(DataTools.LoadSearchList)
to use a method group conversion. -
magnattic about 13 yearsThanks for clearing this up. Will give you the best answer bonus for providing example code.
-
m3z over 10 yearsThanks for introducing me to Lazy<T> - better than writing it out myself which is what i'd always done.