How to pass parameter to static class constructor?

50,991

Solution 1

Don't use a static constructor, but a static initialization method:

public class A
{
    private static string ParamA { get; set; }

    public static void Init(string paramA)
    {
        ParamA = paramA;
    }
}

In C#, static constructors are parameterless, and there're few approaches to overcome this limitation. One is what I've suggested you above.

Solution 2

As per MSDN, A static constructor is called automatically to initialize the class before the first instance is created. Therefore you can't send any parameters.

CLR must call a static constructor, how will it know which parameters to pass it?

So don't use a static constructor.

Here's the work around for your requirement.

public class StaticClass 
{ 
  private int bar; 
  private static StaticClass _foo;

  private StaticClass() {}

  static StaticClass Create(int initialBar) 
  { 
    _foo = new StaticClass();
    _foo.bar = initialBar; 
    return _foo;
  } 
}

Static constructors have the following properties:

  • A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
  • If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
Share:
50,991
MrProgram
Author by

MrProgram

Updated on September 06, 2021

Comments

  • MrProgram
    MrProgram almost 3 years

    I have a static class with a static constructor. I need to pass a parameter somehow to this static class but I'm not sure how the best way is. What would you recommend?

    public static class MyClass {
    
        static MyClass() {
            DoStuff("HardCodedParameter")
        }
    }
    
  • meJustAndrew
    meJustAndrew about 6 years
    but what is the point in using a method anyway, since there is a setter available...
  • Matías Fidemraizer
    Matías Fidemraizer about 6 years
    @meJustAndrew Because that setter is private. Did you notice that important detail? :D
  • Matías Fidemraizer
    Matías Fidemraizer about 6 years
    @meJustAndrew Maybe another approach would be ParamA { private get; set; }... BTW it's just a code snippet to show you would initialize a static class with one or more parameters. Probably an unary constructor would be useless but just think that it's still useful if you want to cover the use case of giving it to some delegate...
  • meJustAndrew
    meJustAndrew about 6 years
    indeed, I haven't noticed that the property was private. I tried to find a way to initialize a static readonly field, from outside the class, and so I found your answer. Unfortunately it doesn't seem to be such a mechanism in C# right now. Sorry again, it was my fault for the comment above!
  • Matías Fidemraizer
    Matías Fidemraizer about 6 years
    @meJustAndrew No problem :D Anyway, you can simulate what you want providing a Lazy<T> instance instead of the value
  • M.Hassan
    M.Hassan over 5 years
    The method Create should be public to be called and initialize StaticClass .
  • Stokely
    Stokely over 2 years
    Can you explain your code a bit more? It appears to me that this is just a Singleton minus the null check to see if _foo is an object. Without that null check this would allow all callers of Create() to keep orphaning objects of StaticClass in memory as new instances are assigned to the static variable _foo, am I right? Thanks!