ReSharper warns: “Static field in generic type”

http://stackoverflow.com/questions/9647641/resharper-warns-static-field-in-generic-type

It's fine to have a static field in a generic type, so long as you know that you'll really get one field per combination of type arguments. My guess is that R# is just warning you in case you weren't aware of that.

Here's an example of that:

using System;

public class Generic<T>
{
    // Of course we wouldn't normally have public fields, but...
    public static int Foo;
}

public class Test
{
    public static void Main()
    {
        Generic<string>.Foo = 20;
        Generic<object>.Foo = 10;
        Console.WriteLine(Generic<string>.Foo); // 20
    }
}

 

As you can see, Generic<string>.Foo is a different field from Generic<object>.Foo - they hold separate values.

 

你可能感兴趣的:(ReSharper warns: “Static field in generic type”)