Static constructor in C#

Solution 1:

The static constructor has no access modifier: it is just:

static DataManager() // note no "public"
{
    LastInfoID = 1;
}

This is because it is never called explicitly (except perhaps via reflection) - but is invoked by the runtime; an access-level would be meaningless.

Solution 2:

The problem is that the LastInfoID field or property is not declared as static in your class and you can access only static members from a static constructor. Also remove the public keyword from the declaration:

static DataManager()
{
    LastInfoID = 1;
}

Solution 3:

Remove the public. The syntax for a static constructor is:

class MyClass 
{
    static MyClass() 
    {
        // Static constructor
    }
}

Solution 4:

To give anyone here a more clear answer, without an example, think about why you would have to access a static constructor from outside? Static classes are created in memory upon application execution, that is why they are static. In other words, you would NEVER need to call one explicitly and if you do, say through reflection (which I don't know if it will let you), then you are doing something wrong.

When you create a new instance of a class, the constructor exists as a way to initialize all he internal variables and to do any kind of processing necessary to make the class function the way it is intended. Notice, if you do not specify a constructor the compiler will create one for you. For this reason you still need to create a class with a "()" like so

     new MyClass();

because you are calling the default constructor (provided you do not have a parameterless one defined). In other words, the reason why a non-static constructor has to be defined as public is because you need to call it explicitly. If memory serves me well, C# will not compile on code that attempts to call a (through malloc) constructor that is not defined as public.

Constructors in a static class exist for "setup" purposes. For instance I can have a static class that is supposed to be the bridge between my code and a file that I am constantly saving and reading data from. I can define a constructor that, upon object creation, will make sure that the file exists and if not creates a default one (very help full in web systems that are ported to other servers).

Solution 5:

using System;

public class Something
{
    //
    private static  DateTime _saticConstructorTime;
    private         DateTime _instanceConstructorTime;
    //
    public static DateTime SaticConstructorTime
    {
        set { _saticConstructorTime = value; }
        get { return _saticConstructorTime ; }
    }
    public DateTime InstanceConstructorTime
    {
        set { _instanceConstructorTime = value; }
        get { return _instanceConstructorTime; }
    }
    //Set value to static propriety 
    static Something()
    {
        SaticConstructorTime = DateTime.Now;
        Console.WriteLine("Static constructor has been executed at: {0}",
                        SaticConstructorTime.ToLongTimeString());
    }
    //The second constructor started alone at the next instances
    public Something(string s)
    {
        InstanceConstructorTime = DateTime.Now;
        Console.WriteLine("New instances: "+ s +"\n");
    }
    public void TimeDisplay(string s)
    {
        Console.WriteLine("Instance \""+ s + "\" has been created at: " + InstanceConstructorTime.ToLongTimeString());
        Console.WriteLine("Static constructor has been created at: " + SaticConstructorTime.ToLongTimeString() + "\n");
    }
}
//
class Client
{
    static void Main()
    {
        Something somethingA = new Something("somethingA");
        System.Threading.Thread.Sleep(2000);
        Something somethingB = new Something("somethingB");

        somethingA.TimeDisplay("somethingA");
        somethingB.TimeDisplay("somethingB");
        System.Console.ReadKey();
    }
}
/* output :

Static constructor has been executed at: 17:31:28
New instances: somethingA

New instances: somethingB

Instance "somethingA" has been created at: 17:31:28
Static constructor has been created at: 17:31:28

Instance "somethingB" has been created at: 17:31:30
Static constructor has been created at: 17:31:28
 */