Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 8 EXPRESSIONS AND OPERATORS

class Program {

static void Main() {

LimitedInt li1 = new LimitedInt(); LimitedInt li2 = new LimitedInt(); LimitedInt li3 = new LimitedInt(); li1.TheValue = 10; li2.TheValue = 26;

Console.WriteLine(" li1: {0}, li2: {1}" , li1.TheValue, li2.TheValue);

li3 = -li1;

Console.WriteLine("-{0} = {1}" , li1.TheValue, li3.TheValue);

li3 = li2 - li1;

Console.WriteLine(" {0} - {1} = {2}" ,

li2.TheValue, li1.TheValue, li3.TheValue);

li3 = li1 - li2;

Console.WriteLine(" {0} - {1} = {2}" ,

li1.TheValue, li2.TheValue, li3.TheValue);

}

}

This code produces the following output:

li1: 10, li2: 26 -10 = 0

26 - 10 = 16

10 - 26 = 0

The typeof Operator

The typeof operator returns the System.Type object of any type given as its parameter. From this object, you can learn the characteristics of the type. (There is only one System.Type object for any given type.) You cannot overload the typeof operator. Table 8-17 lists the operator’s characteristics.

The typeof operator is unary.

Table 8-17. The typeof Operator

Operator Description

typeof

Returns the System.Type object of a given type

 

 

236

CHAPTER 8 EXPRESSIONS AND OPERATORS

The following is an example of the syntax of the typeof operator. Type is a class in the System namespace.

Type t = typeof ( SomeClass )

For example, the following code uses the typeof operator to get information on a class called SomeClass and to print the names of its public fields and methods.

using System.Reflection; // Use the Reflection namespace to take full advantage // of determining information about a type.

class SomeClass

{

public int Field1; public int Field2;

public void Method1() { }

public int Method2() { return 1; }

}

class Program

{

static void Main()

{

Type t = typeof(SomeClass); FieldInfo[] fi = t.GetFields(); MethodInfo[] mi = t.GetMethods();

foreach (FieldInfo f in fi) Console.WriteLine("Field : {0}" , f.Name);

foreach (MethodInfo m in mi) Console.WriteLine("Method: {0}" , m.Name);

}

}

237

CHAPTER 8 EXPRESSIONS AND OPERATORS

The output of this code is the following:

Field : Field1

Field : Field2

Method: Method1

Method: Method2

Method: ToString

Method: Equals

Method: GetHashCode

Method: GetType

The typeof operator is also called by the GetType method, which is available for every object of every type. For example, the following code retrieves the name of the type of the object:

class SomeClass

{

...

}

class Program

{

static void Main()

{

SomeClass s = new SomeClass();

Console.WriteLine("Type s: {0}" , s.GetType().Name);

}

}

This code produces the following output:

Type s: SomeClass

Other Operators

The operators covered in this chapter are the standard operators for the built-in types. There are other special usage operators that are dealt with later in the book, along with their operand types. For example, the nullable types have a special operator called the null coalescing operator, which is described in Chapter 25 along with a more in-depth description of nullable types.

238

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]