Question

The MSDN RyuJIT blog entry gives this instruction for setting up CTP3:

Tricky thing necessary until RyuJIT is final: Add a reference to Microsoft.Numerics.Vectors.Vector to a class constructor that will be invoked BEFORE your methods that use the new Vector types. I’d suggest just putting it in your program’s entry class’s constructor. It must occur in the class constructor, not the instance constructor.

I'm far better versed in class/instance construction in Objective-C than I am in C#. Is he talking about a different concept? What is the difference between a class constructor and an instance constructor in C#? Is the "class constructor" in this case simply the parameterless constructor?

Était-ce utile?

La solution 2

Class constructor = Static constructor

Instance constructor = Normal constructor

For example,

class MyClass
{
    // Static/Class constructor.
    // Note: Static constructors cannot have visibility modifier (eg. public/private),
    //       and cannot have any arguments.
    static MyClass()
    {
        ... // This will only execute once - when this class is first accessed.
    }

    // Normal/Instance Constructor.
    public MyClass(...)
    {
        ... // This will execute each time an object of this class is created.
    }
}

So as an example, consider the following code:

static void Main(string[] args)
{
    var a = new MyClass();  // Calls static constructor, then normal constructor.
    a.DoSomething();
    var b = new MyClass();  // Calls normal constructor only.
    b.DoSomething();
}

Also, consider the following code:

static void Main(string[] args)
{
    MyClass.SomeStaticMethod();       // Calls static constructor, then SomeStaticMethod().
    MyClass.SomeOtherStaticMethod();  // Calls SomeOtherStaticMethod() only.

    // Note: None of the above calls the normal constructor.
}

Autres conseils

I think this refers to the static constructor

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top