The static-constructor tag has no wiki summary.
0
votes
1answer
22 views
Database.SetInitializer() in a static constructor?
I have a very quick question regarding using Entity Framework 5. 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 ...
2
votes
2answers
81 views
How to ensure that a static constructors is called without calling any member
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 ...
0
votes
4answers
157 views
Design issue: static class only initializes once, breaks unit testing
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 values. ...
2
votes
1answer
100 views
Static constructor in abstract class?
Consider the following sample situation:
public abstract class Parent
{
private ByteBuffer buffer;
/* Some default method implementations, interacting with buffer */
public static ...
1
vote
3answers
254 views
System.TypeInitializationException: The type initializer for 'Tips' threw an exception
I am trying to access the values of a static list. However when I try to do so this exception is thrown.
System.TypeInitializationException: The type initializer for 'Tips' threw an exception. -- ...
0
votes
1answer
90 views
How to detect if static constructor was executed in .net?
I'm testing complex .net/COM application under Intel Inspector (native memory leaks). In quite a few places in .net code we are caching references to COM objects in static fields. Obviously Inspector ...
4
votes
5answers
174 views
How to check if a static constructor has been called?
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, except ...
1
vote
3answers
650 views
Private vs Static constructors in .Net
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 ...
6
votes
3answers
101 views
Why isn't a static constructor invoked on a class used as a generic type parameter?
Given the following classes:
public class Foo {
static Foo() {
Console.WriteLine("Foo is being constructed");
}
}
public class Bar {
public void ...
1
vote
1answer
829 views
Explicitly call static constructor
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 ...
4
votes
4answers
297 views
Why can I change a private static readonly field but not a public one?
having code like this:
public static readonly bool MaximumRecipientsReached;
private static readonly IList<EmailAddress> Contacts;
static AdditionalRecipient()
{
...
12
votes
3answers
359 views
How can static constructors be made non-private?
Access modifiers like public, private are not allowed on static constructors in C#. Yet, Visual Studio code analysis has a warning in C# security category that says "CA2121: Static constructors should ...
2
votes
3answers
306 views
unit test static constructor w/ different config values
I have a class with a static constructor which I use to read the app.config values. How do I unit test the class with different configuration values. I'm thinking of running each test in different app ...
0
votes
1answer
122 views
Tasks and Threads don't run within static constructors in Linqpad
This code waits indefinitely on the t.Wait() line.
void Main()
{
Foo.Bar();
}
public static class Foo
{
static Foo()
{
var t = Task.Factory.StartNew (() => 1);
...
4
votes
2answers
410 views
Race condition in c# static constructor
I was debating with a friend who states that the static constructor could give way to a race condition as the static constructor could be called multiple times. It seems this could only happen in high ...
0
votes
1answer
944 views
WCF Service class static constructor called in every call
I have a Service class implementing a contract as follows:
interface IContractFoo
{
void Foo();
}
Class ServiceFoo() : IContractFoo
{
public static ServiceFoo()
{
Log("Static ...
64
votes
10answers
4k views
How does a static constructor work?
namespace MyNameSpace
{
static class MyClass
{
static MyClass()
{
//Authentication process.. User needs to enter password
}
public static void ...
12
votes
2answers
357 views
How does C# know when to run a static constructor?
I don't believe the generated code would check if the class has been initialized everytime it access a static member (which includes functions). I believe checking every access would be inefficient. I ...
2
votes
1answer
513 views
VS2010 did not break debug on static constructor exception
I have a Windows Forms application with a single Editor class (that inherits from Form).
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
Load += ...
3
votes
1answer
79 views
Why the order of entering static .ctors is different from instance .ctors in inherited classes?
Why var b = new B() firstly enters static B() .ctor and than static A() .ctor and not vice versa like the instance constructors does (public A() and than public B())?
public class A
{
static A() ...
3
votes
6answers
3k views
Pass argument to a static constructor in Java?
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 to ...
4
votes
4answers
164 views
Constructor in a class of static methods
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 ...
1
vote
2answers
218 views
Can a static constructor reduce the performance of accessing static methods?
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 check ...
3
votes
2answers
790 views
in C# does Static constructor run for each initialization of object, or only once?
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, ...
3
votes
5answers
346 views
How to trigger a static constructor
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 ...
2
votes
4answers
359 views
Why aren't all static constructors called in C# (i.e. those of the parent classes)?
I have three classes, Base, Derived and Final. Derived derives from Base and Final derives from Derived. All three classes have a static constructor. Class Derived as a public static method called ...
2
votes
3answers
2k views
c# static constructor not called from derived class
class Bus<T>
{
static Bus()
{
foreach(FieldInfo fi in typeof(T).GetFields())
{
if(fi.FieldType == typeof(Argument))
{
...
5
votes
4answers
532 views
.Net : Do static constructors get called when a constant is access?
So here is what I'm thinking...
public class MyClass
{
public const string MyConstant = "MyConstantValue";
private static MyClass DefaultInstance;
static MyClass()
{
...
3
votes
2answers
221 views
Is there a standard way for .NET class loaders to work?
Is there a standard way for .NET class loaders to work?
Say I compile this code:
Option Strict On : Option Explicit On
Module Module1
Sub Main()
...
3
votes
3answers
490 views
Why Are Parentheses Required on C# Static Constructors?
Consider:
class Foo
{
static Foo()
{
// Static initialisation
}
}
Why are the () required in static Foo() {...}? The static constructor must always be parameterless, so why ...
7
votes
1answer
1k views
override metadata in static constructor?
I have a class that inherits the TextBox Class, call it MyTextBox
I'd like to redefine the default Background value for this class.
So I looked for a way to do so and found a good option: call ...
3
votes
6answers
2k views
Imitate a static constructor in C++
This a question related to the initialization of objects in C++
I have a group of classes (not instances), inheriting from a common base class, and I need them to register info about themselves in a ...
14
votes
5answers
2k views
What is the rationale for not having static constructor in C++?
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++
...
10
votes
1answer
476 views
Static constructor on a .NET interface is not run
You can define a static constructor on an interface in .NET in IL. However, if you do so, the static constructor is not run when you run a method on the interface:
.method public static void Main() {
...
1
vote
1answer
312 views
In what order are the static constructors of parent and child classes called?
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 : ...
0
votes
4answers
1k views
Tracking Static Constructor Execution
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 ...
20
votes
6answers
2k views
What's the best way to ensure a base class's static constructor is called?
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 ...
29
votes
3answers
12k views
What is the use of static constructors?
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?
5
votes
2answers
520 views
How do I explicitly run the static constructor of an unknown type? [duplicate]
Possible Duplicate:
How do I invoke a static constructor with reflection?
I've got some initialization code in the static constructor of various classes. I can't create instances, nor do I ...
2
votes
2answers
131 views
Strange behaviour with static fields
I am trying to get a custom enum class working which should enable me to create enums with user friendly identifiers and an arbitrary associated value. so far so good:
public class EnumBase<T, ...
4
votes
2answers
781 views
Forcing class load
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 ...
2
votes
3answers
2k views
C# Instance Constructor vs Static Constructor
What are the differences between the two? I've only used one kind of constructor and I believe it's the static constructor. Only familiar with C++ and Java.
3
votes
3answers
525 views
C# static garbage collector?
I have a simple class which has a static constructor and a instance constructor. Now when i initialized the class , both static and instance constructor are called. Only static is referred once in a ...
1
vote
3answers
211 views
How do I implement a static dictionary<T> with parameters at Runtime in C#?
I have the following code:
public static class ScraperMasterUriDetails
{
public static Dictionary<Guid, string> MasterUriDetails;
}
However I've decided that I need to add ...
1
vote
3answers
496 views
What is the earliest entrypoint that the CLR calls before calling any method in an assembly?
In the past years I've occasionally been wondering what equivalent of the (in)famous DLL_PROCESS_ATTACH was available in the .NET world. Any documentation I have says, slightly simplified, that the ...
33
votes
7answers
751 views
Why doesn't the CLR always call value type constructors
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 ...
5
votes
3answers
1k views
Public constructor and static constructor
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?
1
vote
1answer
908 views
Freestanding ARM C++ Code - empty .ctors section
I'm writing C++ code to run in a freestanding environment (basically an ARM board). It's been going well except I've run into a stumbling block - global static constructors.
To my understanding the ...
2
votes
3answers
2k views
Passing static parameters to a class
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 ...
3
votes
1answer
672 views
Is RunClassConstructor guaranteed to run a type's static constructor only once?
I'm calling the static ctor of a class using this code:
Type type;
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);
Can this cause the cctor to be run twice?