// Error! Mixed types!
var d = new[] { 1, "one", 2, "two", false };
Implicit Typed Data Is Strongly Typed Data
Be very aware that implicit typing of local variables results in strongly typed data. Therefore, use of
the var keyword is not the same technique used with scripting languages (such as VBScript or Perl)
or the COM Variant data type, where a variable can hold values of different types over its lifetime in
a program (often termed “dynamic typing”).
Rather, type inference keeps the strongly typed aspect of the C# language and affects only the
declaration of variables at compile time. After that point, the data point is treated as if it were
declared with that type; assigning a value of a different type into that variable will result in a com-
pile-time error:
static void ImplicitTypingIsStrongTyping()
{
Do'stlaringiz bilan baham: |