Soheil Alizadeh Soheil Alizadeh - 1 month ago 9
C# Question

Why are static methods not available in Singleton Instance?

I create generic type class of name

. For implement Singelton Design Pattern. So, below code is it the class:


public class SingletonGenerator<T> where T : class, new()
private static readonly Lazy<T> _instance =
new Lazy<T>(() => new T(), LazyThreadSafetyMode.ExecutionAndPublication);

private SingletonGenerator()

public static T Instance => _instance.Value;

And, create other class for get instance:


public class AppDbContext
public string Database { get; set; }
private static string ConnectionString { get; set; }

public static void Send()

public void Go()

In Program.cs

class Program
static void Main(string[] args)
var context = SingletonGenerator<AppDbContext>.Instance;

var database = context.Database; // is available
var connection = context.ConnectionString; //is not available

context.Go(); // is available
context.Send(); // is not available

My Question is Why are static methods not available in Singleton Instance?

And My code is correct?

Answer Source

A static member cannot be referenced through an instance. Instead, it is referenced through the type name.

From the docs.

The problem has nothing to do with the fact your instance is a singleton, to reference a static member use the type name: