C# struct new StructType() vs default(StructType)

后端 未结 5 1446
情深已故
情深已故 2021-01-31 01:44

Say I have a struct

public struct Foo
{
    ...
}

Is there any difference between

Foo foo = new Foo();

and <

5条回答
  •  日久生厌
    2021-01-31 02:16

    For value-types, the options are, practically speaking, equivalent.

    However, I was intrigued by Jon Skeet's empirical research into which 'instructions' result in the invocation of a struct's parameterless default constructor when it is specified in CIL (you can't do it in C# because it doesn't let you). Amongst other things, he'd tried out default(T) and new T() where T is a type parameter. They appeared equivalent; neither of them appeared to call the constructor.

    But the one case (it appears) he hadn't tried was default(Foo) where Foo is an actual struct type.

    So I took his code for the 'hacked' struct and tried that out for myself.


    It turns out that default(Foo) doesn't call the constructor, whereas new Foo() in fact does.

    Using a struct type Oddity that specifies a parameterless constructor:

    With optimizations turned off, the method:

    private void CallDefault()
    {
        Oddity a = default(Oddity);
    }
    

    produces the CIL (without nops, rets etc.):

    L_0001: ldloca.s a
    L_0003: initobj [Oddity]Oddity
    

    whereas the method:

    private void CallNew()
    {
        Oddity b = new Oddity();
    }
    

    produces:

    L_0001: ldloca.s b
    L_0003: call instance void [Oddity]Oddity::.ctor()
    

    With optimizations turned on, the compiler appears to optimize away pretty much all of the CallDefault method into a no-op, but keeps the call to the constructor in CallNew (for potential side-effects?).

提交回复
热议问题