[ACCEPTED]-Static constructor in C#-constructor

Accepted answer
Score: 110

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

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

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

Score: 5

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

static DataManager()
{
    LastInfoID = 1;
}
Score: 5

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

class MyClass 
{
    static MyClass() 
    {
        // Static constructor
    }
}
Score: 5

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

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

     new MyClass();

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

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

Score: 1
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
 */

0

More Related questions