In the class definition you often read:
Thread Safety
Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.
But do not be very susceptible for these assurances. Sometimes you read this, because of help template had this text.
Let's look at Encoding::GetEncoding static method:
public static Encoding GetEncoding(string name)
{
return Encoding.GetEncoding(EncodingTable.GetCodePageFromName(name));
}
and
internal static int GetCodePageFromName(
string name)
{
if (
name ==
null)
throw new ArgumentNullException(
"name");
object obj1 =
EncodingTable.
hashByName[
name];
if (
obj1 !=
null)
return (
int)
obj1;
name =
name.
ToLower(
CultureInfo.
InvariantCulture);
obj1 =
EncodingTable.
hashByName[
name];
if (
obj1 !=
null)
return (
int)
obj1;
int num1 =
EncodingTable.
internalGetCodePageFromName(
name);
EncodingTable.
hashByName[
name] =
num1;
return num1;
}
You see now, in the least successful case, when our encoding isn't still cached, we shall cache it in the EncodingTable.hashByName.
I just want to point out that hashtable write operation isn't thread safe.