static-constructor

Static constructor can run after the non-static constructor. Is this a compiler bug?

自古美人都是妖i 提交于 2019-11-27 09:15:35
The output from the following program is: Non-Static Static Non-Static Is this a compiler bug? I expected: Static Non-Static Non-Static because I thought the static constructor was ALWAYS called before the non-static constructor. I tested this with Visual Studio 2010 using both .net 3.5 and .net 4.0. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StaticConstructorBug { class Program { static void Main(string[] args) { var mc = new MyClass(); Console.ReadKey(); } } public class MyClass { public MyClass() { Console.WriteLine("Non-static"); }

How can I run a static constructor?

雨燕双飞 提交于 2019-11-27 05:28: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. 回答1: The other answers are

How does a static constructor work?

偶尔善良 提交于 2019-11-26 22:30:15
问题 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?

Returning in a static initializer

[亡魂溺海] 提交于 2019-11-26 22:01:34
问题 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 回答1: I think the reason is that initializers are carried

What is the use of static constructors?

自作多情 提交于 2019-11-26 21:17:32
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? No you can't overload it; a static constructor is useful for initializing any static fields associated with a type (or any other per-type operations) - useful in particular for reading required configuration data into readonly fields, etc. It is run automatically by the runtime the first time it is needed (the exact rules there are complicated (see "beforefieldinit"), and changed subtly between CLR2 and CLR4). Unless you abuse reflection, it is guaranteed to

static readonly field initializer vs static constructor initialization

a 夏天 提交于 2019-11-26 19:18:34
问题 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; } } 回答1: There is

Static constructor can run after the non-static constructor. Is this a compiler bug?

天大地大妈咪最大 提交于 2019-11-26 14:38:42
问题 The output from the following program is: Non-Static Static Non-Static Is this a compiler bug? I expected: Static Non-Static Non-Static because I thought the static constructor was ALWAYS called before the non-static constructor. I tested this with Visual Studio 2010 using both .net 3.5 and .net 4.0. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StaticConstructorBug { class Program { static void Main(string[] args) { var mc = new MyClass();

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

假如想象 提交于 2019-11-26 11:32:59
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 members of the class. It only gets run once (as the variables are read only and only need to be set once) and since it's a function of the class it can access its private members. I could add code in the constructor that checks to see if the vector is initialized, and initialize it if it's not, but that introduces many necessary checks and doesn't seem

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

强颜欢笑 提交于 2019-11-26 02:27:42
问题 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 members of the class. It only gets run once (as the variables are read only and only need to be set once) and since it\'s a function of the class it can access its private members. I could add code in the constructor that checks to see if the vector