CHAPTER 3 CORE C# PROGRAMMING CONSTRUCTS, PART I
static void LocalVarDeclarations()
{
Console.WriteLine("=> Data Declarations:");
// Local variables are declared and initialized as follows:
// dataType varName = initialValue;
int myInt = 0;
// You can also declare and assign on two lines.
string myString;
myString = "This is my character data";
}
Console.WriteLine();
It is also permissible to declare multiple variables of the same underlying type on a single line of
code, as in the following three bool variables:
static void LocalVarDeclarations()
{
Console.WriteLine("=> Data Declarations:");
int myInt = 0;
string myString;
myString = "This is my character data";
}
// Declare 3 bools on a single line.
bool b1 = true, b2 = false, b3 = b1;
Console.WriteLine();
Since the C# bool keyword is simply a shorthand notation for the System.Boolean structure, it is also
possible to allocate any data type using its full name (of course, the same point holds true for any C#
data type keyword). Here is the final implementation of LocalVarDeclarations(), which illustrates
various ways to declare a local variable.
static void LocalVarDeclarations()
{
Console.WriteLine("=> Data Declarations:");
// Local variables are declared and initialized as follows:
// dataType varName = initialValue;
int myInt = 0;
string myString;
myString = "This is my character data";
// Declare 3 bools on a single line.
bool b1 = true, b2 = false, b3 = b1;
// Use System.Boolean data type to declare a bool.
System.Boolean b4 = false;
Console.WriteLine("Your data: {0}, {1}, {2}, {3}, {4}, {5}",
myInt, myString, b1, b2, b3, b4);
88