We have not
used any type for the variable. In this case, the compiler takes care of defining the
variable type from the value assigned to it. The variable iCount is considered as an
integer as the value assigned to it is an integer. So for any variable to be an implicitly
typed variable, it should have an initializing value assigned to it, and it cannot have
null value assigned to it. As the type is defined by the initial value, the initial value
cannot be changed over the lifetime of the program. If we do so, we will end up
getting an error while compiling.
We can also use implicit typing for declaration of collections. This is very useful
when instantiating complex generic types. For example, the normal way of declaring
a collection which holds item numbers is given as follows:
List
itemNumbers = new List();
itemNumbers.Add(100005);
itemNumbers.Add(100237);
itemNumbers.Add(310078);
The equivalent for the above declaration, using implicit typing, would be as follows:
var itemNumbers = new List();
itemNumbers.
Pages:
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38