.Net : Do static constructors get called when a constant is access? - c#

So here is what I'm thinking...
public class MyClass
{
public const string MyConstant = "MyConstantValue";
private static MyClass DefaultInstance;
static MyClass()
{
DefaultInstance = new MyClass();
}
}
...
NotificationService.RegisterForNotification(MyClass.MyConstant, Callback);
Will this work or do I need to use something like a static readonly property field to trigger the static constructor?

Use of a constant doesn't necessarily result in a member access which would cause the static constructor to be called. The compiler is allowed to (encouraged, even) substitute the value of the constant at compile time.
Your suggested workaround of static readonly should be ok, although readonly suggests a field, not a property. Properties are read-only when they have no setter, the readonly keyword isn't involved.
A simple example:
class HasSConstructor
{
internal const int Answer = 42;
static HasSConstructor()
{
System.Console.WriteLine("static constructor running");
}
}
public class Program
{
public static void Main()
{
System.Console.WriteLine("The answer is " + HasSConstructor.Answer.ToString());
}
}
Output under .NET 4.0:
The answer is 42
The static constructor never runs!

Static constructor is called automatically before the first instance is created or any static members are referenced. See here: MSDN: Static Constructors
By the way, constant fields are inherently static, but as pointed they may (and probably will) be substituted by the value itself.

The static constructor will not be called if you're just accessing public constants. For example, consider this class:
class Testo
{
public const string MyValue = "Hello, world";
public static readonly int xyzzy;
static Testo()
{
Console.WriteLine("In static constructor");
xyzzy = 27;
}
}
Now, in another class, execute:
Console.WriteLine(Testo.MyValue);
Console.WriteLine(Testo.xyzzy);
Your output will be:
Hello, world
In static constructor
27
Constants are hoisted from the class at compile time. So the value of Testo.MyValue does not reference the Testo class at runtime. The static constructor is not called until you call something that requires initialization.
So, yes, you need to access something like a static readonly if you want to ensure that the constructor is called.

No, you don't need such. Static constructor is called when the class is loaded.

Related

Is there a way to mark a function as only called/callable from a static constructor?

I have quite a sizeable static constructor for one of my classes, and was wanting to refactor it to encapsulate various bits of the initialization within static functions.
One of the things this static constructor does a lot of is initialize the values of static readonly fields. However, when I try to move these parts into functions, obviously the compiler wont let me as these can only be set in the static constructor. This makes sense as it doesn't know that I only intend to call these functions from my static constructor.
Is there any way around this? e.g. is there some sort of attribute I can put on the functions to tell the compiler the functions are only to be called from the static constructor?
You can define local functions inside a static constructor;
public class MyClass {
public static readonly int Value;
static MyClass(){
Value = Calc(1);
return;
int Calc(int a) => a;
}
}
The C# compiler will turn those local functions into static methods, with unpronounceable names.
You can do it with static and private keywords
using System;
public class User {
public static readonly string Name;
static User(){
Name = GetName();
}
//`private` function to only be called in static class itself
private static string GetName() {
return "Testing";
}
}
public class Program
{
public static void Main()
{
Console.WriteLine(User.Name);
}
}
A side note for this solution is that it can help you to prevent outsider functions' calls but cannot prevent being called internally by inner functions.

How to pass parameter to static class constructor?

I have a static class with a static constructor. I need to pass a parameter somehow to this static class but I'm not sure how the best way is.
What would you recommend?
public static class MyClass {
static MyClass() {
DoStuff("HardCodedParameter")
}
}
Don't use a static constructor, but a static initialization method:
public class A
{
private static string ParamA { get; set; }
public static void Init(string paramA)
{
ParamA = paramA;
}
}
In C#, static constructors are parameterless, and there're few approaches to overcome this limitation. One is what I've suggested you above.
As per MSDN, A static constructor is called automatically to initialize the class before the first instance is created. Therefore you can't send any parameters.
CLR must call a static constructor, how will it know which parameters to pass it?
So don't use a static constructor.
Here's the work around for your requirement.
public class StaticClass
{
private int bar;
private static StaticClass _foo;
private StaticClass() {}
static StaticClass Create(int initialBar)
{
_foo = new StaticClass();
_foo.bar = initialBar;
return _foo;
}
}
Static constructors have the following properties:
A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to
initialize the class before the first instance is created or any
static members are referenced.
A static constructor cannot be called directly.
The user has no control on when the static constructor is executed in the program.
A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary
method.
If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for
the lifetime of the application domain in which your program is
running.
If by "HardCodedParameter" you really mean hard coded, you can use constants.
public static class YoursClass
{
public const string AnotherHardCodedParam = "Foo";
}
public static class MyClass
{
private const string HardCodedParam = "FooBar";
static MyClass()
{
DoStuff(MyClass.HardCodedParam);
DoStuff(YoursClass.AnotherHardCodedParam);
}
}
Also, you can use static readonly properties.
Constructors on non-static class have have the benefit to ensure they're properly initialized before they're actually being used.
Since static classes don't have this benefit, you have to make ensure that yourself.
Use a static constructor with an obvious name, then in the relevant portion of your static procedures check to make sure the initialization has been performed.
The example below assumes your want to "initialize" your static class with a Form object.
public static class MyClass
{
private static Form FormMain { get; set; }
public static void Init(Form initForm)
{
FormMain = initForm;
}
private static bool InitCheck()
{
return FormMain != null ? true: false;
}
public static void DoStuff()
{
if (InitCheck())
{
// Do your things
}
else
{
throw new Exception("Object reference not set to an instance of an object");
}
}
}

How is the method called in static class

My code is as follows
class MyStaticClass
{
static MyStaticClass{};
public static readonly MyStaticClass Instance = CreateMe();
public static int GetSomeValue = GetValue();
private static int GetValue()
{
return 0;
}
private static MyStaticClass CreateMe()
{
Console.WriteLine("This method was called");
return new MyStaticClass();
}
}
public class Program
{
public static void Main()
{
int val=MyStaticClass.GetSomeValue;
}
}
O/p:
This method was called
When I call val why does the debugger accesses CreateMe method ? Is it that any static method I access it will access all the static methods in the class ?
The method CreateMe() is called because you are calling in when you create object Instance in the following statement.
public static readonly MyStaticClass Instance = CreateMe();
This is static object in your class and is created as you access the class which you did by MyStaticClass.GetSomeValue.
Dubugging the code will give you clear view of the order of the statements get executed. You can go through this detailed article on MSDN regarding debugging Debugger Roadmap
You have a static initializer for a static field. As part of program startup, all static fields are evaluated.
Edit: small clarification here:
The static fields in a particular class are evaluated in declaration order, but there in no particular order for which class has it's static fields initialized first.
Now, if you had a static property, that would be different.
MSDN link
Both fields have been initialized using the static methods. So, in that case, all the static methods will be evaluated.

Class Initialize() in C#?

In Obj-c there is the static Initialize method which is called the first time that class is used, be it statically or by an instance. Anything like that in C#?
You can write a static constructor with the same syntax as a normal constructor, except with the static modifier (and no access modifiers):
public class Foo {
static Foo() {
// Code here
}
}
Usually you don't need to do this, however - static constructors are there for initialization, which is normally fine to do just in static field initializers:
public class Foo {
private static readonly SomeType SomeField = ...;
}
If you're using a static constructor to do more than initialize static fields, that's usually a design smell - but not always.
Note that the presence of a static constructor subtly affects the timing of type initialization, requiring it to be executed just prior to the first use - either when the first static member access, or before the first instance is created, whichever happens first.
There is a static constructor. As per msdn:
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
public class Foo
{
static Foo() {} //static constructor
}
Yes, it is called a constructor.
By MSDN:
public class Taxi
{
public bool isInitialized;
//This is the normal constructor, which is invoked upon creation.
public Taxi()
{
//All the code in here will be called whenever a new class
//is created.
isInitialized = true;
}
//This is the static constructor, which is invoked before initializing any Taxi class
static Taxi()
{
Console.WriteLine("Invoked static constructor");
}
}
class TestTaxi
{
static void Main()
{
Taxi t = new Taxi(); //Create a new Taxi, therefore call the normal constructor
Console.WriteLine(t.isInitialized);
}
}
//Output:
//Invoked static constructor
//true

How to return a static class instance in c#

I would like to get an instance of a static class, but I can’t seem to do this without implementing a singleton wrapper on a non-static class– is this possible, or am I missing something?
public class MyInstanceTester
{
public MyInstanceTester()
{
//this is how i get a reference to a singleton now
MyClass instance1 = MyClass.Instance();
//this is what is would like to do (if only the compiler would let me)
MyStaticClass instance2 = MyStaticClass.Instance();
}
}
public class MyClass
{
private static MyClass _myInstance;
static MyClass()
{
_myInstance = new MyClass();
}
public static MyClass Instance()
{
return _myInstance;
}
}
public static class MyStaticClass
{
public static MyStaticClass Instance
{
get
{
return this;
}
}
}
There is no such thing as an instance of a static class. The singleton pattern simply returns the same instance of a class to repeated requests.
You may be getting confused by:
private static MyClass _myInstance;
This simply means that there will be a single instance of that particular object among all objects instantiated of the type that have _myInstance as a member.
A few notes:
The this keyword is not valid in a static member
If you have a static class then all members have to be static and so this will never be valid
A Singleton class cannot be a static class
Singletons declare a single static member to help ensure that only a single instance of that class exists
Note that a static reference to an object does not make the object static. Only the reference is static
Further reading: Jon Skeet has an excellent write up on implemeting Singletons in C# In Depth. I would suggest reading and studying this article until you grok it. It is quite good.
There is no reason to return a instance to a static class ( If the class is static there is no instance ).
You can access the class from everywhere, why returning a instance to it? I can't imagine any reason to do this.
Static class usage
To use a static class just write it like below:
MyStaticClass.MyMethod();
Int32 callCount = MyStaticClass.CallCount;
As you can see it doesn't even make sense to declare a variable because this would just look like this:
MyStaticClass msc = MyStaticClass.Instance();
msc.MyMethod();
Int32 callCount = msc.CallCount;
If you want to have a shorter name you just can use:
using MSC = MyNamespace.MyStaticClass;
From your comments I assume your solution would be:
Make your class non-static. (Just keep the methods static.)
Your terminology is wrong. Please read the MSDN article on the static keyword.
A static member cannot be referenced through an instance. Instead, it
is referenced through the type name.
A singleton is a class that only allows a single instance of itself. A common implimentation of this in C# is:
public class MyClass
{
private MyClass _value = null;
public MyClass Value {
get { return _value ?? (_value = new MyClass()); }
}
}
The major problem is here:
public static class MyStaticClass
{
public static MyStaticClass Instance
{
get
{
return this; //compile time error!
}
}
}
this refers to an instance of a class which does not make sense in a static class as there can be no instance of one. This by itself should make you realize that there is a fundamental error in what you are asking: "I would like to get an instance of a static class". You can not return an instance of a static class as a static class by definition can not be instantiated.
The singleton pattern just makes sure that you always return the same instance of a class. But said class can never be static.

Categories

Resources