#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

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: