Alex Rivera | Logout

what's the difference between the default and default constructor

Asked 2010-11-28T01:51:24.520
12

I have a class named A. What the difference between these two statements?

A a = new A();

A a = default(A);
Edit
Report

1 Answer

14

This creates a new instance of the type A by calling the default, parameterless constructor:

A a = new A();

This assigns the default value for type A to the variable a and does not call any constructor at all:

A a = default(A);

The main difference is that the default value of a type is null for reference types and a zero-bit value for all value types (so default(int) would be 0, default(bool) would be false, etc.).

answered 2010-11-28T01:54:38.333

Your Answer