Subscribe

RSS Feed (xml)

Showing posts with label Variables. Show all posts
Showing posts with label Variables. Show all posts

A Closer Look at Variables in C#

Variables are declared using this form of statement:

type var-name;

where type is the data type of the variable and var-name is its name. You can declare a variable of any valid type, including the value types just described. When you create a variable, you are creating an instance of its type. Thus, the capabilities of a variable are determined by its type. For example, a variable of type bool cannot be used to store floating-point values. Furthermore, the type of a variable cannot change during its lifetime. An int variable cannot turn into a char variable, for example.

All variables in C# must be declared prior to their use. This is necessary because the compiler must know what type of data a variable contains before it can properly compile any statement that uses the variable. It also enables C# to perform strict type-checking.

C# defines several different kinds of variables. The kind that we have been using are called local variables because they are declared within a method.

Initializing a Variable

You must give a variable a value prior to using it. One way to give a variable a value is through an assignment statement, as you have already seen. Another way is by giving it an initial value when it is declared. To do this, follow the variable’s name with an equal sign and the value being assigned. The general form of initialization is shown here:

type var = value;

Here, value is the value that is given to var when var is created. The value must be compatible with the specified type.

Here are some examples:

int count = 10; // give count an initial value of 10
char ch = 'X';  // initialize ch with the letter X
float f = 1.2F; // f is initialized with 1.2

When declaring two or more variables of the same type using a comma-separated list, you can give one or more of those variables an initial value. For example:

int a, b = 8, c = 19, d; // b and c have initializations

In this case, only b and c are initialized.

Dynamic Initialization

Although the preceding examples have used only constants as initializers, C# allows variables to be initialized dynamically, using any expression valid at the time the variable is declared. For example, here is a short program that computes the hypotenuse of a right triangle given the lengths of its two opposing sides:

// Demonstrate dynamic initialization.

using System;

class DynInit {
 public static void Main() {
   double s1 = 4.0, s2 = 5.0; // length of sides

   // dynamically initialize hypot
   double hypot = Math.Sqrt( (s1 * s1) + (s2 * s2) );

   Console.Write("Hypotenuse of triangle with sides " +
                 s1 + " by " + s2 + " is ");

   Console.WriteLine("{0:#.###}.", hypot);

 }
}

Here is the output:

Hypotenuse of triangle with sides 4 by 5 is 6.403.

Here, three local variables—s1, s2, and hypot—are declared. The first two, s1 and s2, are initialized by constants. However, hypot is initialized dynamically to the length of the hypotenuse. Notice that the initialization involves calling Math.Sqrt( ). As explained, you can use any expression valid at the time of the declaration. Since a call to Math.Sqrt( ) (or any other library method) is valid at this point, it can be used in the initialization of hypot. The key point here is that the initialization expression can use any element valid at the time of the initialization, including calls to methods, other variables, or literals.

C# The bool Type

The bool type represents true/false values. C# defines the values true and false using the reserved words true and false. Thus, a variable or expression of type bool will be one of these two values. Unlike some other computer languages, in C# there is no conversion defined between bool and integer values. For example, 1 does not convert to true, and 0 does not convert to false.

Here is a program that demonstrates the bool type:

// Demonstrate bool values.

using System;

class BoolDemo {
 public static void Main() {
   bool b;

   b = false;
   Console.WriteLine("b is " + b);
   b = true;
   Console.WriteLine("b is " + b);

   // a bool value can control the if statement
   if(b) Console.WriteLine("This is executed.");

   b = false;
   if(b) Console.WriteLine("This is not executed.");

   // outcome of a relational operator is a bool value
   Console.WriteLine("10 > 9 is " + (10 > 9));
 }
}

The output generated by this program is shown here:

b is False
b is True
This is executed.
10 > 9 is True

There are three interesting things to notice about this program. First, as you can see, when a bool value is output by WriteLine( ), “True” or “False” is displayed. Second, the value of a bool variable is sufficient, by itself, to control the if statement. There is no need to write an if statement like this:

if(b == true) ...

Third, the outcome of a relational operator, such as <, is a bool value. This is why the expression 10 > 9 displays the value “True.” Further, the extra set of parentheses around 10 > 9 is necessary because the + operator has a higher precedence than the >.

The C# Class Library

The sample programs shown in this chapter make use of two of C#’s built-in methods: WriteLine( ) and Write( ). As mentioned, these methods are members of the Console class, which is part of the System namespace, which is defined by the .NET Framework’s class library. As explained earlier in this chapter, the C# environment relies on the .NET Framework class library to provide support for such things as I/O, string handling, networking, and GUIs. Thus, C# as a totality is a combination of the C# language itself, plus the .NET standard classes. As you will see, the class library provides much of the functionality that is part of any C# program. Indeed, part of becoming a C# programmer is learning to use these standard classes.

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail