March 2019
Intermediate to advanced
242 pages
6h 21m
English
Using var with primitive data types seems to be the simplest scenario, but appearances can be deceptive. Try to execute the following code:
var counter = 9_009_998_992_887; // code doesn't compile
You might assume that an integer literal value (9_009_998_992_887, in this case) that doesn't fit into the range of primitive int types will be inferred to be a long type. However, this doesn't happen. Since the default type of an integer literal value is int, you'll have to append the preceding value with the suffix L or l, as follows:
var counter = 9_009_998_992_887L; // code compiles
Similarly, for an int literal value to be inferred as a char type, you must use an explicit cast, as follows:
var aChar = (char)91; ...
Read now
Unlock full access