static-constructor

Database.SetInitializer() in a static constructor?

戏子无情 提交于 2019-12-23 22:01:44
问题 Many are perhaps aware of why we need to use the code shown below. However, I want to separate this logic into layers and I don't want to reference the Entity Framework DLL in my web layer, thus I ended up putting this code in a static constructor of my DbContext class. Is this a bad idea? Will there be a performance hit on the app by doing this? Database.SetInitializer<DataContext<T>>(null); 回答1: There is no performance hit that is worth to be mentioned. A static constructor is called once

Why isn't the static constructor of the parent class called when invoking a method on a nested class?

*爱你&永不变心* 提交于 2019-12-23 10:48:15
问题 Given the following code, why isn't the static constructor of "Outer" called after the first line of "Main"? namespace StaticTester { class Program { static void Main( string[] args ) { Outer.Inner.Go(); Console.WriteLine(); Outer.Go(); Console.ReadLine(); } } public static partial class Outer { static Outer() { Console.Write( "In Outer's static constructor\n" ); } public static void Go() { Console.Write( "Outer Go\n" ); } public static class Inner { static Inner() { Console.Write( "In Inner

How does the CLR handles static classes?

烂漫一生 提交于 2019-12-22 09:43:59
问题 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 understanding, we use constructors only for instantiating the class) 回答1: First of all there is no static class in CLR. CLR doesn't know anything about static class. It is the feature of C#. Static classes are compiled into abstract as well as sealed class. Making it abstract prevent instantiation

Passing static parameters to a class

冷暖自知 提交于 2019-12-19 02:47:31
问题 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? 回答1: 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

What is the use of static constructors?

会有一股神秘感。 提交于 2019-12-17 06:21:48
问题 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? 回答1: 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

Using Static Constructor (Jon Skeet Brainteaser)

徘徊边缘 提交于 2019-12-14 03:43:13
问题 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 to ask if something has changed in recent versions and to see what output others are getting from this code. The question is, "What will be displayed, why, and how confident are you?" using System; class Foo { static Foo() { Console.WriteLine ("Foo");

Constructor in a class of static methods

倖福魔咒の 提交于 2019-12-12 12:12:01
问题 I've got a class of static methods that can be performed on a map held within the class, and I want the map to be set up when the class is called. I've tried using a private contructor, but it isn't being called. The relevant parts of my code are: public class MyClass { private static final String KEYS = "ABC"; private static final String[] DATA = {"AAA", "BBB", "CCC"}; private static HashMap<Character, String> myMap; private MyClass() { System.out.println("Running constructor");

In what order are the static constructors of parent and child classes called?

巧了我就是萌 提交于 2019-12-11 10:49:46
问题 In what order are the static constructors of parent and child classes called? class A { static A() { MessageBox.Show("Yaht"); } } class B : A { static B() { MessageBox.Show("Zee"); } } class C : A { static C() { MessageBox.Show("Zey"); } } static void Main() { B b = new B(); C c = new C(); } I could test it right now... if I had a compiler available. 回答1: Output: Zee Yaht Zey .......... 来源: https://stackoverflow.com/questions/5240011/in-what-order-are-the-static-constructors-of-parent-and

Why the default constructor executed before the static constructor?

浪尽此生 提交于 2019-12-11 07:45:57
问题 I am wondering why my static constructor is outputting default constructor Static Constructor , and not the other way around Static Constructor and Default constructor or just Default constructor . When I use a static constructor, it should execute the static constructor first. However, from the code below, The First question: why is the default constructor is called before the static constructor? class Program { static void Main(string[] args) { var test = Single.S; } class Single{ static

Why Are Parentheses Required on C# Static Constructors?

删除回忆录丶 提交于 2019-12-10 20:26:45
问题 Consider: class Foo { static Foo() { // Static initialisation } } Why are the () required in static Foo() {...} ? The static constructor must always be parameterless, so why bother? Are they necessary to avoid some parser ambiguity, or is it just to maintain consistency with regular parameterless constructors? Since it looks so much like an initialiser block, I often find myself leaving them out by accident and then have to think for a few seconds about what is wrong. It would be nice if they