#1,014 – Using default Operator in a Generic Class

The default operator provides a valid default value for any type.

One place where this operator is very handy is in a generic class, operating on the type parameter T.  In the example below, we initialize an internal collection of type T so that each element has the proper default value.

    public class BagOf<T>
    {
        private Collection<T> coll;
        public T SomeItem
        {
            get { return coll[0]; }
        }

        public BagOf(int numInBg)
        {
            if (numInBg <= 0)
                throw new Exception("Must have >0 items");

            coll = new Collection<T>();
            for (int i = 0; i < numInBg; i++)
                coll.Add(default(T));
        }
    }

Using this class, we can see the default values for each type of item.

            // Numeric
            BagOf<int> bagOfInt = new BagOf<int>(2);
            int anInt = bagOfInt.SomeItem;

            // Struct
            BagOf<Point3D> bagOfPoints = new BagOf<Point3D>(3);
            Point3D aPoint = bagOfPoints.SomeItem;

            // Reference type
            BagOf<Dog> bagOfDogs = new BagOf<Dog>(4);
            Dog aDog = bagOfDogs.SomeItem;

1014-001

Advertisements

#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

#175 – The default Clause of a switch Statement

Normally, if the value of the expression in a switch statement doesn’t match any of the values listed in the case clauses, control falls through the switch statement and none of the clauses are executed.

Optionally, you can provide a default clause, which will get executed if none of the case clauses are satisfied.

            switch (surname)       // surname is a string
            {
                case "Aarhus":
                case "Enevoldsen":
                    Console.WriteLine("Norwegian");
                    break;
                case "Brosnan":
                case "McGill":
                    Console.WriteLine("Irish");
                    break;
                case "Afonso":
                case "Silva":
                    Console.WriteLine("Portuguese");
                    break;
                default:
                    Console.WriteLine("UNKNOWN origin");
                    break;
            }