Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic static classes have multiple instances?

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).

like image 219
T-moty Avatar asked Mar 15 '16 09:03

T-moty


People also ask

Can generic classes be static?

1 Answer. You can't use a class's generic type parameters in static methods or static fields. The class's type parameters are only in scope for instance methods and instance fields.

Can you have multiple instances of a static class in Java?

A static class is a nested class (i.e. it is declared within another class). It behaves like a top level class, which means you can create multiple instances of it.

What is functionality of static class?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

What are static classes in C#?

In static class, you are not allowed to create objects. In non-static class, you are allowed to create objects using new keyword. The data members of static class can be directly accessed by its class name. The data members of non-static class is not directly accessed by its class name.


2 Answers

Yes, on the fly a non-generic version of the generic class is generated. That means that every static variable is static within the context of the generated non-generic version (yes, another Type) of your generic class.

To work around this intended behavior, you could create an singleton pattern class outside the generic class where you put all static variables in that should be shared across all versions.

like image 190
Patrick Hofman Avatar answered Sep 20 '22 12:09

Patrick Hofman


Patrick already got the solution, I just add some information.

Actually when your class is static there are no instances of it at all, no matter if the class is generic or not. However what you mean is that all the implementations of the generic class - for instance MyClass<int>, MyClass<string> and so on - are completely different types that are compiled to different classes and do not know anything on each other, they do not even implement the same base-class.

like image 38
MakePeaceGreatAgain Avatar answered Sep 21 '22 12:09

MakePeaceGreatAgain