jeudi 17 mai 2018

Why the c# Activator can instanciate a struct without providing constructor parameters [duplicate]

This question already has an answer here:

Let's say I have such struct:

public struct Dog
{
    public Dog(string name)
    {
        Name = name;
    }
    public string Name { get; set; }
}

Why does the following code not crash ?

var dog = Activator.CreateInstance<Dog>();

If Dog was a class there would be a runtime error. But with a struct the instance is created with a null Name.





Aucun commentaire:

Enregistrer un commentaire