Closed Generic Types Have Their Own Statics

You really have to be careful when you use generic types. Say you have a generic type Foo<T> like this:

public class Foo<T>
{
  public static string StaticValue = "Unset.";
}

Fine, right? Well, what happens if you do this?

Console.WriteLine("Foo<string>: {0}", Foo<string>.StaticValue);
Console.WriteLine("Foo<int>: {0}", Foo<int>.StaticValue);
Foo<string>.StaticValue = "String value.";
Foo<int>.StaticValue = "Integer value.";
Console.WriteLine("Foo<string>: {0}", Foo<string>.StaticValue);
Console.WriteLine("Foo<int>: {0}", Foo<int>.StaticValue);

I bet you can guess what the output is.

Foo<string>: Unset.
Foo<int>: Unset.
Foo<string>: String value.
Foo<int>: Integer value.

That's right - each closed generic type (a generic that has a type parameter provided) has its own set of static values.

Think about how that can bite you - if you have multiple parameters on your generic type, like MyType<T, U, V>, and you have three different types that are used in each of T, U, and V, you end up with 27 sets of static values (it's combinatoric). Possibly not what you're thinking at the time you write the code.

I really can't find any documentation on this. There's a nice CodeProject article about it that shows why this can really work against you if you're trying to make a strongly-typed generic singleton factory, but beyond that, MSDN really only yields up an FxCop rule that's fairly unrelated.

Be careful with your generics and static values!

Print | posted @ Thursday, July 17, 2008 10:44 AM

Comments on this entry:

Gravatar # re: Closed Generic Types Have Their Own Statics
by Guido Domenici at 7/22/2008 8:33 AM

I was also baffled by this a few days ago, but then I looked directly in the C# 2.0 Language Spec, and it confirmed what you say above (page 18):

"A static variable in a generic class declaration is shared amongst all instances of the same closed constructed type (§20.5.2), but is not shared amongst instances of different closed constructed types. These rules apply regardless of whether the type of the static variable involves any type parameters or not."

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 8 and 2 and type the answer here: