|Written by Nikos Vaggalis|
|Thursday, 18 November 2010|
Page 2 of 6
The previous example highlights the importance of widening and narrowing conversions between related types. However this does not mean that strongly typed languages cannot convert between unrelated types.
This can be achieved by using conversion classes such as System.Convert :
string mystring = "10";
or by extending the language by providing user defined implicit or explicit conversions from and to:
or by operator overloading.
Let's take a look at operator overloading in C#:
C# turned weakly typed?
How could this implicit conversion, in example 17, from double to string be possible with a strongly typed language?
Let's a take a look at what the MSDN Library has to say on the "+" operator :
“Binary + operators are predefined for numeric and string types. For numeric types, + computes the sum of its two operands. When one or both operands are of type string, + concatenates the string representations of the operands"
There is a built-in overload of the plus operator which can make it work with numbers and strings, and that is why I deliberately used the minus '-' operator, which is not overloaded, in an earlier C# example. In the earlier Example 5 using the '+' operator could lead to wrong assumptions - that C# is weakly typed instead of strongly, although it acted as a weak type in that case.
However, if we turn the left value of the assignment from string to double:
a = a + x;
we now get a compile time error:
Error 1 Cannot implicitly convert type 'string' to 'double'
C# being a static language has all the type information beforehand and can check the validity of the statement at compile time.
In this case we have a string and double - can we add them? It seems the “+” operator overload allows that but the result of the addition is a string; may we then assign the result to a double variable? No, we may not. It's complicated - and if you don't believe me try explaining this strange set of rules to a beginner!
An untyped language by nature does not have compile time type information but this does not mean that it does not do any type checks; those checks are deferred to until runtime since the type information is not available until then.
The equivalent of the C# Example 5 with the subtraction operator '-', in VB.NET would compile successfully and fail at runtime:
C# Perl-like behavior?
If you need Perl-like dynamic behavior from C# you could alter the previous example by using the dynamic type as:
which produces the runtime error:
double c = a + x;
It does compile but fails at runtime since by using the dynamic type, all type checking is deferred until runtime where the actual types are discovered and subsequently the operation is checked for its validity
On the other hand, the following not only compiles but also executes with no runtime exception since the DLR infers that the type of variable 'd' should be that of a string:
dynamic d = a + x; //7.3hi
And as a matter of fact, we can get to the actual runtime type assigned to the variable with:
(As a side note, the 'dynamic' keyword which is bound to a runtime mechanism , is very different from the type inference 'var' keyword which is bound to a compile time mechanism.)
|Last Updated ( Friday, 09 November 2012 )|