Subscribe

RSS Feed (xml)

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.

No comments:

Post a Comment

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail