CHAPTER 3 CORE C# PROGRAMMING CONSTRUCTS, PART I
you can determine the data type of an implicitly typed local variable. Update your method with the
following code statements:
static void DeclareImplicitVars()
{
// Implicitly typed local variables.
var myInt = 0;
var myBool = true;
var myString = "Time, marches on...";
// Print out the underlying type.
Console.WriteLine("myInt is a: {0}", myInt.GetType().Name);
Console.WriteLine("myBool is a: {0}", myBool.GetType().Name);
Console.WriteLine("myString is a: {0}", myString.GetType().Name);
}
Note Be aware that you can use this implicit typing for any type including arrays, generic types (see Chapter 9),
and your own custom types. You’ll see other examples of implicit typing over the course of this book.
If you were to call the DeclareImplicitVars() method from within Main(), you’d find the output
shown here.
***** Fun with Implicit Typing *****
myInt is a: Int32
myBool is a: Boolean
myString is a: String
Restrictions on Implicitly Typed Variables
There are various restrictions regarding the use of the var keyword. First and foremost, implicit typing
applies only to local variables in a method or property scope. It is illegal to use the var keyword to define
return values, para