Recent development in my technical life is that, started working out with Orcas Beta 2. So, thought to blog about the latest happenings with C# language. The idea emerged to start a series of posts related to C# new features. This is the first of ever such kind of blogging specific to a topic.
C# 3.0 has many-a-new features. To start with, let me take a concept of Implicitly Typed Variables.
Implicitly Typed Variables
In the good old days, the developer has to worry about the type of the variable. Say for instance, whether to use long or double for a counter. Here all that we observe is that the language that is built upon is the type specific. Hence forth the developer is not required to define the type of the variable at the time of declaration, but it is the task of the compiler to decide what type of the object the variable is. All that the developer has to do is that, use the var keyword while declaring the variable, similar to that of JScript or Visual Basic style. Hey!!! Stop!!!!! don't get confuse with the type of VAR variables declared at JScript or Visual basic.
Let's first discuss the difference between VAR variables at JScript and VAR variables of C#
|VAR JScript||VAR C#|
|This is of no type||The type of the variable is defined by the value declared and decided at the compile time|
|Technically have no type. Can consider of limited types, namely, string literal, numeric, boolean||Type agnostic, have specific predefined formats|
|Type conversion is coercion||Type casting is simple and handled by CLR|
|No mechanism for parsing||explicit functions for parsing to specific type|
Now, let us see the difference between the language specific VAR of VisualBasic 6.0 and C# 3.0
|VAR in VB (but not .NET)||VAR in C# 3.0|
|By definition, these are Variant||Type of the variable is defined at the compile time|
|Could be any allowed type from with in the known types of the language||Type is decided by the value associated with the variable|
|Largest among all the known data types||Size depends on the type of the value initialised|
To summarize, the variables declared in C# 3.0 are type specific, thou used the key word VAR, during the declaration. Thus, we can conclude that the compiler is the responsible point to decide the type of the variable. Hence we can say with comfort that, the variables from C# 3.0 are Implicitly Typed variables.
Some examples as mentioned below.
var vIntVal = 10; // This will be the System.Int32 type
var vLongVal = 10000000000; // This will be the System.Int64 type
var vDoubleVal = 10.0; // This will be the System.Double type
var vFloatVal = 10.0f; // This will be the System.Single type
float vFlVal = 10.0f; // Thou defined using float key word, but inherits from Struct System.Single
var vStrVal = "String Value "; // This will be the System.String type
So, from the above declarations, it is pretty clear that the variable is defined by the value associated during the declaration. The type is not just limited to the kind of data types as explained above, but you can extend this to any type of the variable that you use while writing code for iterations, similar such as foreach. Below is the example for other known types.
foreach (var vTable in ds.Tables) // Implicitly declared a variable of Data Table Type
foreach (var vRow in ((DataTable) vTable).Rows) // Implicit declaration of DataRow variable
By using such, one can extend any extent. The limit is the imagination of the developer. What do you say?