Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

Default Values

You learned earlier that C# does not allow you to use an uninitialized variable, which means the variable must have a value before you use it. Although this idea of definite assignment helps reduce errors, because it is enforced by the compiler, it can be cumbersome if you have to explicitly provide a default value for every field.

To alleviate this burden, fields, or member variables, are always initially assigned with an appropriate default value. Table 3.12 shows the default value for the different predefined data types.

Table 3.12. Default Values

Type

Default

sbyte, byte, short, ushort, int, uint, long, ulong

0

char

'\x0000'

float

0.0f

double

0.0d

decimal

0.0m

bool

false

object

null

string

null

As you can see, for the integral value types, the default value is zero. The default value for the char type is the character equivalent of zero and false for the bool type. The object and string types have a default value of null, representing a null reference that literally is one that does not refer to any object.

  • + Share This
  • 🔖 Save To Your Account