#1,013 – default Operator Returns Default Values

The default operator can be used to determine the default value for any type.  It returns values as follows:

  • For value types – the result of zeroing out the contents of the variable
    • Numeric types – zero
    • bool type – false
    • char type – null character
    • struct types – all fields defaulted
  • For reference types – null

Here are some examples:

            int n1 = default(int);          // 0
            double d1 = default(double);    // 0.0
            bool b1 = default(bool);        // false
            char c1 = default(char);        // \0
            string s1 = default(string);    // null
            Dog d = default(Dog);           // null  (ref type)
            Point3D p1 = default(Point3D);  // X=0.0, Y=0.0, Z=0.0, Name=null

1013-001

Advertisements