Questions tagged [static-constructor]

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

Such a pattern is provided, for example, by the language.

More Info

141 questions
377
votes
8 answers

What is the use of static constructors?

Please explain to me the use of static constructor. Why and when would we create a static constructor and is it possible to overload one?
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
192
votes
23 answers

static constructors in C++? I need to initialize private static objects

I want to have a class with a private static data member (a vector that contains all the characters a-z). In java or C#, I can just make a "static constructor" that will run before I make any instances of the class, and sets up the static data…
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
83
votes
10 answers

How does a static constructor work?

namespace MyNameSpace { static class MyClass { static MyClass() { //Authentication process.. User needs to enter password } public static void MyMethod() { //Depends on…
om471987
  • 5,398
  • 5
  • 32
  • 40
56
votes
10 answers

How can I run a static constructor?

I'd like to execute the static constructor of a class (i.e. I want to "load" the class) without creating an instance. How do I do that? Bonus question: Are there any differences between .NET 4 and older versions? Edit: The class is not static. I…
mafu
  • 31,798
  • 42
  • 154
  • 247
49
votes
4 answers

static readonly field initializer vs static constructor initialization

Below are two different ways to initialize static readonly fields. Is there a difference between the two approaches? If yes, when should one be preferred over the other? class A { private static readonly string connectionString = …
stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92
44
votes
4 answers

How to pass parameter to static class constructor?

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() { …
MrProgram
  • 5,044
  • 13
  • 58
  • 98
42
votes
4 answers

Explicitly call static constructor

I want to write unit test for below class. If name is other than "MyEntity" then mgr should be blank. Negative Unit test Using Manager private accessor I want to change name to "Test" so that mgr should be null. And then will verify the mgr…
meetjaydeep
  • 1,752
  • 4
  • 25
  • 39
39
votes
6 answers

What's the best way to ensure a base class's static constructor is called?

The documentation on static constructors in C# says: A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
38
votes
7 answers

Why doesn't the CLR always call value type constructors

I have a question concerning type constructors within a Value type. This question was inspired by something that Jeffrey Richter wrote in CLR via C# 3rd ed, he says (on page 195 - chapter 8) that you should never actually define a type constructor…
jameschinnock
  • 767
  • 6
  • 14
31
votes
5 answers

What is the rationale for not having static constructor in C++?

What is the rationale for not having static constructor in C++? If it were allowed, we would be initializing all the static members in it, at one place in a very organized way, as: //illegal C++ class sample { public: static int some_integer; …
Nawaz
  • 353,942
  • 115
  • 666
  • 851
20
votes
5 answers

Using Static Constructor (Jon Skeet Brainteaser)

As a relative newbie I try to read as much as I can about a particular subject and test/write as much code as I can. I was looking at one of Jons Brainteasers (question #2) and my output was different than the answer. Which makes brings me here…
Leroy Jenkins
  • 2,680
  • 6
  • 25
  • 33
18
votes
4 answers

Public constructor and static constructor

I am reading a code in C# that uses two constructors. One is static and the other is public. What is the difference between these two constructors? And for what we have to use static constructors?
Strider007
  • 4,615
  • 7
  • 25
  • 26
14
votes
1 answer

How to ensure that a static constructors is called without calling any member

I have a class with a static constructor. I want the static constructor to be called without calling or using any of its members, but only if the constructor has not been called already. I tried using reflection. With reflection I can invoke the…
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
13
votes
6 answers

Returning in a static initializer

This isn't valid code: public class MyClass { private static boolean yesNo = false; static { if (yesNo) { System.out.println("Yes"); return; // The return statement is the problem } …
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
13
votes
3 answers

How can static constructors be made non-private?

Access modifiers like public, private are not allowed on static constructors in C#. Yet, Visual Studio code analysis has a warning in C# security category that says "CA2121: Static constructors should be private". Is it possible to make a static…
Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
1
2 3
9 10