Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
BASICS OF C# Appendix E 981
Variable Data Types
The data type of a variable defines the type of the variable. Figure E-1 displays the data types in C#.
Data Type |
|
Description of the Data Type |
|
||
|
|
|
int |
|
An int data type is used to store 32-bit signed integer values. |
|
|
|
long |
|
A long data type is used to store 64-bit signed integer values. |
|
|
|
short |
|
A short data type is used to store 16-bit signed integer values. |
|
|
|
sbyte |
|
A sbyte data type is used to store 8-bit signed integer values. |
|
|
|
byte |
|
A byte data type is used to store 8-bit unsigned integer values. |
|
|
|
ushort |
|
An ushort data type is used to store 16-bit unsigned integer values. |
|
|
|
uint |
|
An uint data type is used to store 32-bit unsigned integer values. |
|
|
|
ulong |
|
An ulong data type is used to store 64-bit unsigned integer values. |
|
|
|
float |
|
A float data type is used to store floating point values of |
|
|
single precision. |
|
|
|
double |
|
A double data type is used to store floating poin values of |
|
|
double precision. |
|
|
|
decimal |
|
A decimal data type is used to store 28 digit decimal numbers. |
|
|
|
char |
|
A char data type is used to store a Unicode character value. |
|
|
|
string |
|
A string data type is used to store an array of Uniciode characters. |
|
|
|
object |
|
The object data type is the base type of every type. |
|
|
|
bool |
|
A bool data type is used to store boolean values, true or false. |
|
|
|
FIGURE E-1 Variable data types
Types of Variables
There are seven types of variables in C#:
static. A static variable has a static modifier. You can access a static variable directly from the class to which it belongs. You do not need to create an instance of a class to access a static variable. A static variable
982 |
Part XII |
APPENDIXES |
|
|
|
becomes active when the program in which it is declared is loaded, and becomes inactive when the program terminates.
instance. An instance variable is declared without the static modifier.
array elements. An array element stores the starting address of an array in memory. To access an array element, you need to create an instance of the array.
value parameters. A value parameter is a variable declared without a ref or out modifier. When you call a method that contains the value parameter, the parameter becomes active. It takes the value of the argument that you specify in the method. When the method is returned, the value parameter becomes inactive.
reference parameters. A reference parameter has a ref modifier. A reference parameter is initialized with the value of the underlying variable. A reference parameter stores the location of the argument that is specified when the method is declared.
output parameters. An output parameter is a variable declared with the out modifier. The out modifier allows you to pass a variable, which is not initialized, to a method. The variable then takes the value from the method to which it is passed.
local variables. A local variable is declared within a method. A local variable is not initialized automatically and becomes active when the program that contains the local variable is executed. It becomes inactive when the execution of the immediate code, which contains the local variable, ceases.
Variable Scope
A scope of a variable defines the region of the code within which you can access a variable. Table E-3 lists the scopes supported in C#.
Table E-3 |
Variable Scopes |
|
Variable Scope |
Description |
|
|
|
|
Block |
|
You can access the variable only within the code in which it is declared. |
Procedure |
|
You can access the variable only within the procedure for which it is |
|
|
declared. |
Namespace |
|
You can access the variable from anywhere within the namespace. |
|
|
|
986 |
Part XII |
APPENDIXES |
|
|
|
Similar to integers, you can also use arrays to store character values. An array of character values is called a string. The next section details the use of strings.
String
C# provides you with a String type. In C and C++, an array of characters is called a string. String is not a class in C and C++. Therefore, working with strings is a problem in C and C++. Simple operations, such as comparing or adding two strings, require a lot of programming. To provide the programmers with a solution to this problem, C# created a String class.
You can initialize a string by using the String keyword. To initialize a string, string1, use the following statement:
String string1 = “Hello World”;
The preceding sample code declares a String, string1, and initializes it with a value, “Hello World”.
The assignment of a value to a string takes place by the reference type. When you declare a string, an object of the String class is created and placed on the heap. This object of the String class has the reference to the memory location where the string is stored.
Working with Strings
The String class in C# is in the System namespace. String is a class and has several methods associated with it. You can use these methods to perform operations on strings. The following are the commonly used methods in the String class:
Compare. The Compare method is used to compare two strings.
Format. You can use the Format method to format the values in a string. The Format method allows you to specify formatting for each value in a string.
Trim. The Trim method in C# deletes the extra spaces in a string. The Trim method can be used to delete both the leading and trailing spaces.
ToUpper. To change the capitalization of the elements in a string, you can use the ToLower or ToUpper method. The ToLower method converts the elements of the string to lowercase. Similarly, you can convert the string to uppercase by using the ToUpper method.
988Part XII APPENDIXES
Simple. Simple statements include all declaration and labeled statements. Simple statements also include statements that are used to call methods.
Selection. In cases where the program has to execute one block of statements out of all the available blocks of statements, selection statements are used. The selection statements are of two types, if and switch statements.
Iteration. The iteration statements are used to execute a set of statements repeatedly until a condition is met. The types of iteration statements are for, foreach, while, and do-while statements.
Jump. The jump statements are used to pass the control of the execution unconditionally to another line in the program. The line of code to which the control is transferred is called the target of the jump state-
ment. The commonly used jump statements in C# are goto, return, break, and continue statements.
Proceeding further, I will discuss the types of statements that I cited in each of these categories. I discuss only those types that have a different syntax from C++.
Method Call Statements
A method call statement is also a type of a simple statement. A method call statement is used to call a method that is already created. The following code sample is an example of a method call statement:
String x;
x = “This is a sample Message Box.”;
MessageBox.Show(x);
The preceding code calls the Show method of the class MessageBox.
switch-statement
Switch statements are used the same way as in C++. However, in C and C++, a fall-through condition can occur. In a fall-through condition, if you omit any of the break statements, the program executes two cases. Whereas in C#, a fall-through situation is omitted because the compiler throws an error for each case that does not end with a break statement. The following code in C# generates an error:
int x;
switch (x)
BASICS OF C# Appendix E 989
{
case 1:
MessageBox.Show(“x is a positive number.”);
break;
case 2:
MessageBox.Show(“x is a negative number.”);
default:
MessageBox.Show(“x is equal to 0.”);
break;
}
If you need to execute two cases, you need to provide an explicit goto statement. The syntax for such code is given here:
switch (expression)
{
case 1 : statement goto case2;
case 2 : statement
goto default; [default:
statement jump-statement]
}
In this case, the compiler first executes case1, then case2, and then the default case.
In addition to the selection statements, types of statements also include iteration statements.
foreach Statement
The foreach statement is new in C#. The foreach statement iterates the statements in the foreach loop for each element in an array or collection. The following example will help you to understand the foreach loop:
int [] Integer = {15,89,1000,6};
foreach (int x in Integer)
