Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between the default and default constructor

Tags:

c#

constructor

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

A a = new A();

A a = default(A);
like image 440
user496949 Avatar asked Nov 28 '10 01:11

user496949


1 Answers

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.).

like image 122
Andrew Hare Avatar answered Oct 04 '22 22:10

Andrew Hare