static-constructor

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

荒凉一梦 提交于 2019-11-30 11:22:56
问题 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; static std::vector<std::string> strings; //illegal constructor! static sample() { some_integer = 100; strings.push_back("stack"); strings.push_back("overflow"); } }; In the absense of static constructor, it's very difficult to have static vector, and

Singleton implementation laziness with static constructor

亡梦爱人 提交于 2019-11-30 05:43:53
问题 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 beforefieldinit. However, I did some testing and it seems that it's more lazy without the beforefieldinit. Code sample (the private constructor call outputs to console and verifies that the fields were initialized: public sealed class Singleton { private static readonly Singleton instance = new

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

社会主义新天地 提交于 2019-11-29 23:56:16
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; static std::vector<std::string> strings; //illegal constructor! static sample() { some_integer = 100; strings.push_back("stack"); strings.push_back("overflow"); } }; In the absense of static constructor, it's very difficult to have static vector, and populate it with values, as shown above. static constructor elegantly solves this problem. We could

Why doesn't the CLR always call value type constructors

久未见 提交于 2019-11-29 22:12:04
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 within a value type as there are times when the CLR will not call it. So, for example (well...Jeffrey Richters example actually), I can't work out, even by looking at the IL, why the type constructor is not being called in the following code: internal struct SomeValType { static SomeValType() { Console.WriteLine("This never gets displayed"); }

Why doesn't the CLR always call value type constructors

南楼画角 提交于 2019-11-28 18:34:01
问题 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 within a value type as there are times when the CLR will not call it. So, for example (well...Jeffrey Richters example actually), I can't work out, even by looking at the IL, why the type constructor is not being called in the following code:

How can I run a static constructor?

梦想与她 提交于 2019-11-28 04:52:30
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 want to run it before creating instances because it takes a while to run, and I'd like to avoid this delay at first access. The static ctor initializes private static readonly fields thus cannot be run in a method instead. The other answers are excellent, but if you need to force a class constructor to run without having a reference to the type (ie.

Returning in a static initializer

回眸只為那壹抹淺笑 提交于 2019-11-28 01:50:36
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 } System.exit(0); } } This is a stupid example, but in a static class constructor we can't return; . Why? Are there good reasons for this? Does someone know something more about this? So the reason why I should do return is to end constructing there. Thanks I think the reason is that initializers are carried together with field initializations (and with constructors, in the case of instance initializers). In other

static readonly field initializer vs static constructor initialization

时光怂恿深爱的人放手 提交于 2019-11-27 18:21:11
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 = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } class B { private static readonly string connectionString; static B() { connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } } Mark Byers There is one subtle difference between these two, which can be seen in the IL code - putting an explicit

How does a static constructor work?

帅比萌擦擦* 提交于 2019-11-27 16:52:35
namespace MyNameSpace { static class MyClass { static MyClass() { //Authentication process.. User needs to enter password } public static void MyMethod() { //Depends on successful completion of constructor } } class Program { static void Main(string[] args) { MyClass.MyMethod(); } } } Here is the sequence which I assumed Start of static constructor End of static constructor Start of main Start of MyMethod End of main Now in any scenario if 4 will start before 2 I am screwed. Is it possible? Eric Lippert You only asked one question here but there are a dozen or so questions that you should have

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

寵の児 提交于 2019-11-27 11:54:40
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 created or any static members are referenced . That last part (about when it is automatically called) threw me for a loop; until reading that part I thought that by simply accessing a class in any way , I could be sure that its base class's static constructor had been called. Testing and examining the documentation have revealed that this is not the case; it