All value types implicitly declare a public parameterless constructor called the default constructor. The default constructor returns a zero-initialized instance known as the default value for the value type:
sbyte
, byte
, short
, ushort
, int
, uint
, long
, and ulong
, the default value is 0
.char
, the default value is '\x0000'
.float
, the default value is 0.0f
.double
, the default value is 0.0d
.decimal
, the default value is 0.0m
.bool
, the default value is false
.E
, the default value is 0
.null
.Like any other constructor, the default constructor of a value type is invoked using the new
operator. In the example below, the i
and j
variables are both initialized to zero.
class A { void F() { int i = 0; int j = new int(); } }
Because every value type implicitly has a public parameterless constructor, it is not possible for a struct type to contain an explicit declaration of a parameterless constructor. A struct type is however permitted to declare parameterized constructors. For example
struct Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } }
Given the above declaration, the statements
Point p1 = new Point(); Point p2 = new Point(0, 0);
both create a Point
with x
and y
initialized to zero.