I’m trying to figure out if .NET 4.0’s Enum.TryParse is thread safe.
The source code (decompiled) is:
[SecuritySafeCritical]
public static bool TryParse<TEnum>(string value, bool ignoreCase, out TEnum result) where TEnum : struct
{
result = default(TEnum); /// (*)
Enum.EnumResult enumResult = default(Enum.EnumResult);
enumResult.Init(false);
bool result2;
if (result2 = Enum.TryParseEnum(typeof(TEnum), value, ignoreCase, ref enumResult))
{
result = (TEnum)enumResult.parsedEnum;
}
return result2;
}
What seems problematic to me is this line:
result = default(TEnum); /// (*)
What if another thread accessed result right after it’s been set to the default value, and before it is set to the parsed value?
[EDIT]
Following Zoidberg’s answer, I’d like to rephrase the question a bit.
The question is, I guess, if Enum.TryParse is “transactional” (or atomic).
Say I have a static field, and pass it into Enum.TryParse:
public static SomeEnum MyField;
....
Enum.TryParse("Value", out MyField);
Now, when TryParse is being executed, another thread accesses MyField. TryParse will change MyField’s value to SomeEnum’s default value for a while, and only then will set it to the parsed value.
This is not necessarily a bug in my code. I would expect Enum.TryParse to either set MyField to the parsed value or not touch it at all, not use it as its temporary field.
result (and thus the variable it refers to) can become default(T), although the string value holds a different enum value if it’s that what you mean.
Try the following program:
It will sooner or later (probably sooner) throw the “Not threadsafe” exception.