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

后端 未结 5 1447
情深已故
情深已故 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:10

    No, both expressions will yield the same exact result.

    Since structs cannot contain explicit parameterless constructors (i.e. you cannot define one yourself) the default constructor will give you a version of the struct with all values zero'd out. This is the same behavior that default gives you as well.

提交回复
热议问题