#184 – Cheating Type Safety with object Type

Since every object in C# derives from System.Object, it’s possible to “cheat” type safety by using the object type and casting objects to the desired type at run-time.

For example, assume we have a method that adds two parameters that are assumed to be numbers:

        public static double AddNums(object n1, object n2)
        {
            double d1 = Convert.ToDouble(n1);
            double d2 = Convert.ToDouble(n2);

            return d1 + d2;
        }

This is convenient because now we can pass in any numeric type we like because we can implicitly cast anything to object.

            int i1 = 5, i2 = 7;
            double d1 = 10.2, d2 = 23.2;

            // These all work as expected
            double sum = AddNums(i1, i2);
            sum = AddNums(d1, d2);
            sum = AddNums(i1, d1);

The problem is that the compiler won’t complain if we try to pass in some non-numeric object.  The following code will compile fine, but throw an exception at run-time.

            string s = "Uh-oh";
            sum = AddNums(s, 1);
Advertisement

#183 – Use var to Tell the Compiler to Figure out the Type

You can use traditional strong-typing in C#, where you explicitly declare the type of every variable.  Or you can use the dynamic keyword for dynamic typing, where data is only type-checked at run-time, rather than compile-time.

You can also use the var keyword when declaring variables.  When you use var, you don’t have to explicitly specify the type–the compiler will figure out the correct type at compile-time.  You’re still using strong-typing in this case, because the type checking is done at compile-time.

The following example still fails to compile.  The compiler figures out that s is of type string, it does type-checking and then complains that the use of Concat is invalid.

            var s = "Sean";
            s = s.Concat(" Sexton");

In the next example, we don’t bother to declare the exact types.

            var s = "Hemingway";
            var backwards = s.Reverse();

            // string
            Console.WriteLine(s.GetType());

            // System.Collections.Generic.IEnumerable<char>
            Console.WriteLine(backwards.GetType());

#181 – C# Is Strongly Typed

C# is a strongly typed language, meaning that every variable and object has a well-defined type.  At compile-time, the compiler checks to make sure that all operations use objects of the correct type.  This generally means that if a function takes an argument of type double, you’ll get a compile-time error if you try to pass it something that is of a different type.

There are many benefits of using dynamically typed languages (e.g. Python, Ruby).  However, the main advantage of a strongly typed language is that errors related to type conversion are caught at compile-time rather than at run-time.  It’s always better to find a bug earlier, rather than later.  Finding a bug at compile-time forces the developer to fix it.  Finding it at run-time means that the bug might only be found by a customer, after the product has shipped.

Exception: the dynamic keyword