Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

76

Part II

HANDLING DATA

 

 

 

Indexers

There may be instances where you need to access the elements of a class as an array. You can do this by using indexers provided by C#. To be able to use indexers in classes, you first need to declare an indexer. Indexers are declared as follows:

<modifier> <type> this [parameter-list]

Here, modifier is the indexer modifier and type defines the return type of the indexer. The this keyword is used as a name of the indexer. Indexers do not have an explicit name. The parameter-list in the square brackets defines the data type of the object that has the elements to be accessed.

TIP

In the indexer declaration statement, you can specify any data type as the index of the elements to be accessed.

For example, consider the following sample code:

public int this [int x]

This code declares a public indexer with the return type as integer. Here, the data type of the object is of the integer type.

C# allows you to define both read-only and write-only indexers. To read and write data to an indexer, you use the get and set properties, respectively. The get and set properties do not take any parameter. However, the get property returns the elements of the type as specified in the indexer declaration statement. The set property is used to assign values to indexer elements.Consider the following code:

class Class1

{

int variable1, variable2;

public int this [int x]

set

{

switch (x)

{

MORE ABOUT COMPONENTS

Chapter 4

77

 

 

 

case 0:

variable1 = 10;

break; case 1:

variable2 = 20;

break;

}

}

get

{

switch (x)

{

case 0:

return variable1;

case 1:

return variable2;

}

}

}

In this code, the switch statements are used to read and write data to the indexer.

In this section, you learned about arrays, collections, and indexers that are used to store and access variables and objects. However, you also need to learn about type casting variables into objects and vice versa. To do this, C# provides you with the techniques of boxing and unboxing.

Boxing and Unboxing

Boxing is a data type conversion technique that is used to implicitly convert a value type to either an object type or a reference type. When you convert a value type to an object type, C# creates an instance of the object type and then copies the value type to that instance.

Consider the following example of an implicit data conversion by using boxing.

class Class1

{

public static void Main ()

78

Part II

HANDLING DATA

 

 

 

{

 

 

 

 

 

 

string string1 = “New String”;

 

 

object obj1 = string1;

 

 

 

Console.WriteLine (obj1);

 

 

}

 

 

}

 

 

 

This code initializes a string type variable with the value “New String” and then

 

 

 

Y

 

creates an instance obj1 of the type object. The value of string1 is now copied to

 

 

L

 

the new instance of object and is displayed in the Console window.

 

 

F

 

In addition to implicit data conversion by using boxing, you can use boxing to

 

 

M

 

explicitly convert data. Look at the following example of an explicit data conver-

sion by using boxing. A string string1 = “New String”;E

object obj1 = (object) string1;

This code uses theTcast operator to explicitly convert string1 to an object.

Similar to boxing, unboxing is also a data type conversion technique. Unboxing is used to explicitly convert an object type to a value type.The technique of unboxing is opposite to that of boxing. However, to unbox a reference type, it is essential that you first box the value type. Unboxing can be only of the explicit conversion type. Consider the following example to understand unboxing.

string string1 = “New String”;

object obj1 = string1;

string string2 =(string) obj1;

While unboxing from one type to another, you need to take care that the resultant variable has enough space to store the initial type. For example, if you try to unbox as byte variable type from an integer variable type, it may result in an error. In this case, you box a 32-bit integer type value to an 8-bit sbyte type value. Subsequently, you unbox a smaller value to a larger value. Therefore, the following code generates an error in C#.

int x = 100;

object y = (object) x;

sbyte z = (sbyte) y;

Team-Fly®

MORE ABOUT COMPONENTS

Chapter 4

79

 

 

 

In addition to data conversion statements, C# provides you with certain commands that influence the compilation of your code. These commands are called preprocessor directives.

Preprocessor Directives

In C#, preprocessor directives are commands that are not executed. However, these commands influence the process of compilation of code. For example, if you do not want the compiler to execute certain part of the code, you can mark the code by using the preprocessor directive. To declare a preprocessor directive, use a # sign, such as:

# preprocessor name

Some of the commonly used preprocessor directives provided by C# are discussed in the following sections.

#region and #endregion

C# provides you with the #region preprocessor directive that you can use to define a set of statements to be executed as a block. The #endregion directive marks the end of such a set of statements. For example:

#region Region1

string EmpName, EmpAddress;

int Empcode, Empphone;

#endregion

Here, Region1 is the name given to the set of statements marked by the #region preprocessor directive.

#define and #undef

The #define and #undef preprocessor directives are used to define and remove the definition of a symbol, respectively. These preprocessor directives are similar to a variable declaration statement. However, the symbols created by these directives

80

Part II

HANDLING DATA

 

 

 

do not exist. You can use the #define and #undef directives to declare symbols. However, you cannot create symbols by using these declarations. For example:

#define symbol1

and

#undef symbol1

The first line of code defines a symbol with the name symbol1, and the second line of code deletes the definition of symbol1.

#if, #endif, #else, and #elif

As discussed earlier, preprocessor directives can be used to prevent a compiler from executing certain sections of code. Similarly, you can also use certain preprocessor directives to conditionally compile certain sections of code. To do this, C# provides you with the #if, #endif, #else, and #elif preprocessor directives. These directives are commonly called conditional preprocessor directives.

The syntax of an #if-#endif command is as follows:

#if symbol1

-----------------

#endif

Here, symbol1 is a symbol declared by the #define preprocessor directive. The statements in the #if loop are executed if the symbol following the #if keyword has been previously declared using the #define command. If symbol1 has not been previously declared, the compiler reaches the end of #endif statement.

You can also direct the compiler to execute a set of statements if the symbol is not defined.This can be done using the #elif and #else preprocessor directives. Look at the following example.

#define Symbol1

class Class1

{

#if Symbol1

Console.WriteLine (“Symbol1 exists”)

MORE ABOUT COMPONENTS

Chapter 4

81

 

 

 

#else

Console.WriteLine (“Symbol1 does not exist”)

#endif

}

TIP

You can also use nested #if-#elif loops.

#error and #warning

The #error and #warning preprocessor directives are used to raise an error and a warning, respectively. If the compiler encounters the #warning preprocessor directive, it issues a warning to the programmer by displaying the text in the #warning statement. The compiler then resumes with compilation of the code. However, if the compiler comes across an #error preprocessor directive, it generates an error and stops executing the code. The #error and #warning preprocessor directives are generally used with the conditional preprocessor directives discussed previously.

Look at the following example to have better understanding of the preprocessor directives used in C#.

#define Symbol1 using System; public class Class1

{

public static void Main()

{

#if Symbol1

Console.WriteLine(“Symbol1 is defined”);

#else

#warning Symbol1 is not defined

#endif

}

}

The output of the previous code is shown in Figure 4-2.