Can a static generic class have more than one single instance?
Standard static classes have a single instance, right? Like this:
public static class MyClass
{
public static string MyString { get; set; }
}
public void ExampleMethod(int id)
{
if (id > 0)
MyClass.MyString = id.ToString();
}
Everywhere in my program, MyClass represent a single instance, that is application scoped.
Ok, but, what if MyClass is generic?
public static class MyClass<T>
{
public static string MyString { get; set; }
public static T MyT { get; set; }
}
Means that for each type argument specified, my application scope will create a new instance? Or it will create a separate instance for each possible type argument? (i really hope that it doesn't)
For logic, it cannot still be a single instance, because i can do:
public void ExampleMethod(int id)
{
MyClass<int>.MyT = id;
MyClass<DateTime>.MyT = DateTime.Now;
MyClass<string>.MyT = "Hello, World";
MyClass<DayOfWeek>.MyT = DayOfWeek.Monday;
}
Thanks in advance for all replies
UPDATE - Microsoft .Net Team already use that
Accidentally, i found an example of usage of a static generic class, built into mscorlib DLL:
// Decompiled with JetBrains decompiler
// Type: EmptyArray`1
// Assembly: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 7D59CE68-D0F6-428F-B71C-C8D703E59C19
// Assembly location: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll
internal static class EmptyArray<T>
{
public static readonly T[] Value = new T[0];
}
The presence of that class means that application-scope will create an empty array, if is not already created for a given type (maybe arrays are memory-hunters objects).