static-constructor Questions

8

Solved

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 y...
Disharmonious asked 14/7, 2010 at 12:33

6

Solved

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

23

Solved

I want to have a class with a private static data member: class C { // read-only, can also be static const // should be filled with all characters from 'a' to 'z' static std::vector<cha...
Noway asked 28/7, 2009 at 22:26

1

Solved

If I compile this code with GCC or Clang and enable -O2 optimizations, I still get some global object initialization. Is it even possible for any code to reach these variables? #include <string&...
Borer asked 11/3, 2022 at 22:48

8

Solved

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?
Tacklind asked 22/12, 2010 at 7:20

4

Solved

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...
Synovia asked 11/12, 2015 at 8:36

4

Solved

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? ...
Shrive asked 8/6, 2010 at 7:26

5

Solved

I'm running into a problem here where a static constructor of one of my classes is being called before it should be. (I.e, DI/IoC isn't set up and it's getting null/exceptions back from the service...
Outermost asked 1/2, 2011 at 19:49

2

Solved

Env.: C#6, Visual Studio 2015 CTP 6 Given the following example: namespace StaticCTOR { struct SavingsAccount { // static members public static double currInterestRate = 0.04; static Savin...

4

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 ...
Apperception asked 17/7, 2012 at 10:42

10

Solved

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...
Pushover asked 16/4, 2010 at 15:9

4

Solved

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 ...
Choice asked 3/5, 2010 at 21:37

1

Solved

Jon Skeet suggests in his singleton implementation that if you require the maximum laziness for your singleton you should add a static constructor which will make the compiler mark the type as befo...
Whitish asked 24/5, 2016 at 14:0

4

Solved

What is the real difference between a C# static constructor and a Java static block? They both must be parameterless. They are both called only once, when the related class is first used. Am I mi...
Discommon asked 17/3, 2010 at 19:30

2

Solved

I am learning C# and JAVA I found Static Constructor in C# which is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automati...
Donofrio asked 24/5, 2015 at 20:2

1

Solved

As described in another SO post of me I saw a strange behaviour of my application after moving from VS 2008 (.net 3.5) to VS 2013 (and using .net 4.0, not 4.5). I found that the static constructor ...

4

Solved

I have a bit of a head-scratcher here that I wonder if someone may know the answer to. The setup is basically this: //in Visual Studio plug-in application SpinUpProgramWithDebuggerAttached(); //...
Arnaud asked 24/1, 2015 at 1:59

3

Solved

A static constructor is executed the first time you access a static member. Knowing this, I have several questions: Does this mean that every time I access a static method, the runtime must chec...
Eam asked 15/11, 2011 at 6:46

2

Solved

Can anyone explain how the CLR handles Static classes? Does the CLR create one singleton instance for handling static classes internally? If not, why do we have a static constructor in C#? (Per my ...
Gehlbach asked 3/9, 2014 at 11:36

2

Solved

Lets say I have 2 classes: public abstract class Foo { static Foo() { print("4"); } } public class Bar : Foo { static Bar() { print("2"); } static void DoSomething() { /*...*/ ...
Semiquaver asked 20/11, 2013 at 9:38

1

Solved

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 ...
Vacuva asked 4/5, 2013 at 16:8

6

Solved

I'm trying to initialize a static class, with an argument, and then run some more static code in that class. I'm aware of the static block, but it seems it can't take any arguments. Is there a way ...
Bourguiba asked 29/1, 2012 at 10:28

4

Solved

I have a static Configuration class responsible for data settings for my entire system. It loads certain values from the registry in its constructor, and all of its methods are based on these value...
Oxidation asked 31/3, 2013 at 14:36

5

I have some classes that caches data from a database, these classes are loaded with data when their static constructor gets called. I need to call a static Reload method at all these classes, exce...
Winnick asked 28/1, 2013 at 15:13

5

code: class Base<T,U> where T:Base<T,U>,new() where U :class { protected static U _val = null; internal static void ShowValue() { if(_val == null)new T(); //Without this line, it w...
Gallicize asked 2/8, 2011 at 6:15

© 2022 - 2024 — McMap. All rights reserved.