Search code examples
c#structnew-operatorvalue-type

If a struct is a value type why can I new it?


In C# structs are value types, but I am able to new them as if they are reference types. Why is this?


Solution

  • Because they have constructors.

    The new operator doesn't mean "this is a reference type"; it means "this type has a constructor". When you new something you create an instance, and in doing so you invoke a constructor.

    For that matter, all value and reference types have constructors (at the very least a default constructor taking no args if the type itself doesn't define any).