static-constructor

in C# does Static constructor run for each initialization of object, or only once?

独自空忆成欢 提交于 2019-12-03 11:19:39
问题 in my Class I have a static dictionary of strings object which contains a big number of Items (it reads from a file and initial them) I wrote a static constructor to do so and it takes a few seconds, but I want to do it once to be faster, since I'm doing it in ASP.Net and I want my website not to have this overhead what should I do? if this constructor runs for each object then I was thinking of some method instead but I guess I have to run this method in each page of website which user runs,

Forcing class load

本秂侑毒 提交于 2019-12-03 11:17:14
Is there a way in C# or .net IL to force a class that has a type initializer (static constructor) to load itself, without accessing any of its parameters? Assuming I've got the class public static class LogInitialization { static LogInitialization() { System.Console.WriteLine("Initialized"); } } Is there a way to get this line to print? Note that the class is static so I can't instantiate it to force initialization, and it has no public members so I can't access them to start it. Rummaging in the CLI spec, I found a reference to the method RuntimeHelpers.RunClassConstructor If a language

Private vs Static constructors in .Net

人走茶凉 提交于 2019-12-03 06:28:23
I searched for this a lot, but none of the answers are clear (at-least for me!). Now I'm putting this question in SO, as I believe I can't get a more clarified answer anywhere else. When should I use a private/static constructor in my class? I'm fed up of usual answers, so please help me with some real-time examples and advantages/disadvantages of using these constructors. Static constructors: used for initialising static members. Private constructors: used when you only want a class to be instantiated from within its own code (typically in a static method). For example: public class Thing {

in C# does Static constructor run for each initialization of object, or only once?

天涯浪子 提交于 2019-12-03 00:49:03
in my Class I have a static dictionary of strings object which contains a big number of Items (it reads from a file and initial them) I wrote a static constructor to do so and it takes a few seconds, but I want to do it once to be faster, since I'm doing it in ASP.Net and I want my website not to have this overhead what should I do? if this constructor runs for each object then I was thinking of some method instead but I guess I have to run this method in each page of website which user runs, so I think again it would be the same, am I right? what's your solution for initialization a big set

Assigning to static readonly field of base class

喜你入骨 提交于 2019-12-01 03:26:57
public class ClassA { public static readonly string processName; } public class ClassB : ClassA { static ClassB() { processName = "MyProcess.exe"; } } I am getting an error while compiling the above C# code. The error says -- "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)" But I am assigning it in a static constructor. The need for such a static variable is that, the base class has methods that uses this variable, but the derived classes and the base class must have different values for this variable. But the value is constant across

Assigning to static readonly field of base class

天涯浪子 提交于 2019-12-01 00:05:36
问题 public class ClassA { public static readonly string processName; } public class ClassB : ClassA { static ClassB() { processName = "MyProcess.exe"; } } I am getting an error while compiling the above C# code. The error says -- "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)" But I am assigning it in a static constructor. The need for such a static variable is that, the base class has methods that uses this variable, but the derived

Singleton implementation laziness with static constructor

耗尽温柔 提交于 2019-11-30 22:11:04
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 Singleton(); public static string Stub() { return "123"; } // Explicit static constructor to tell C# compiler /

Passing static parameters to a class

瘦欲@ 提交于 2019-11-30 21:02:42
As far as I know you can can't pass parameters to a static constructor in C#. However I do have 2 parameters I need to pass and assign them to static fields before I create an instance of a class. How do I go about it? This may be a call for ... a Factory Method! class Foo { private int bar; private static Foo _foo; private Foo() {} static Foo Create(int initialBar) { _foo = new Foo(); _foo.bar = initialBar; return _foo; } private int quux; public void Fn1() {} } You may want to put a check that 'bar' is already initialized (or not) as appropriate. You can't pass parameters to a static

Type initializer (static constructor) exception handling

半城伤御伤魂 提交于 2019-11-30 14:54:07
问题 I'm writing a WCF service in C#. Initially my implementation had a static constructor to do some one-time initialization, but some of the initialization that is being done might (temporarily) fail. It appears that static constructors are only called once, even if the first (failed) attempt threw an exception? Any subsequent attempts to instantiate my class will immediately fail with a TypeInitializationException without the code actually being executed. The C# language specification states

Type initializer (static constructor) exception handling

六眼飞鱼酱① 提交于 2019-11-30 12:23:27
I'm writing a WCF service in C#. Initially my implementation had a static constructor to do some one-time initialization, but some of the initialization that is being done might (temporarily) fail. It appears that static constructors are only called once, even if the first (failed) attempt threw an exception? Any subsequent attempts to instantiate my class will immediately fail with a TypeInitializationException without the code actually being executed. The C# language specification states that a static constructor is called at most once, but basically this makes an exception in there an error