#954 – Static Typing vs. Strongly Typed

There is often confusion in C# between the terms static typing (or statically typed) and strongly typed.

Static typing is the process of enforcing type safety at compile-time, ensuring that you only interact with objects in ways that are allowed by that object’s type.  C# mostly uses static typing to enforce type safety, but also supports dynamic typing, using the dynamic keyword.

The idea of a language being strongly typed is, in general, the idea that a language enforces type safety.  This may be done either statically or dynamically.  People have traditionally considered C# to be a strongly typed language.

The term strongly typed is not all that useful, however.  The term is subjective and means different things to different people.  Different languages may be more or less strongly typed than other languages.  So it doesn’t make sense to say that a language is or is not strongly typed.

#182 – C# is (Mostly) Strongly Typed

Traditionally, C# has been considered as a strongly typed language.  But with the addition of the dynamic keyword in C# 4.0, you can choose to declare and use dynamically typed variables.  These variables are not type-checked at compile time, but only at run-time.

For example, the following code will not compile.  The String.Concat method is being used incorrectly.  (It’s a static method).

            string s = "Et tu";
            s = s.Concat(" Brutus");   // Compile-time error

In the following example, we declare the variable as dynamic, which means that the type of the variable is only determined at run-time.  No type-checking is done at compile-time.  This code will now compile fine.  The error will only be found at run-time, when an exception is thrown.

            dynamic s = "Et tu";
            s = s.Concat(" Brutus");   // This compiles