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

36

Part I

INTRODUCTION TO C#

 

 

 

method must a return a value of the return type. The syntax of the return statement is as follows:

return expression;

Here, return is a keyword that you use to write a return statement.

If the method is of the type void, the return statement does not take an expression.Constructors or destructors also do not require an expression with the return statement.

break Statement

As you have seen, you can use a return statement to end a method. Similarly, to end a loop, you use the break statement. It is used to exit from a loop, such as for, foreach, do, and do-while loop. The break statement passes the control out of the loop. The break statement is written using the break keyword.

break;

For nested loops, the break statement passes the control to the end of the innermost loop.

continue Statement

Similar to a break statement, the continue statement is also used with loops. The continue statement is used to end only the current iteration and not the entire loop. The execution again starts for the next iteration. The continue keyword is used to specify a continue statement.

continue;

A continue statement cannot be used to exit a finally block. You will learn about the finally block in Chapter 6, “Threads.”

Having learned about statements, you need to understand expressions. Expressions are also used to perform operations on variables.

Expressions

Expressions in C# are similar to that of C++. Expressions are defined as a sequence of operands and operators that are used to perform operations. An expression can

C# BASICS

Chapter 2

37

 

 

 

be of the following types: values, variables, classes, namespaces, indexers, and methods.

Operators

Operators are used to write expressions. Operators specify the kind of operation that is to be performed on the operands. The types of operators supported by C# are displayed in Table 2-3.

Table 2-3 The Types of Operators in C#

Types of Operators

Description

Unary operators

Unary operators perform operations on a single operand. For

 

example, the ++ and -- operators are unary operators. Unary oper-

 

ators can either precede or succeed the operand.Unary operators

 

are used with numeric data t ypes.

Binary operators

Binary operators perform operations on two operands. For exam-

 

ple, +, -, +=, -=,*, and / are binary operators. Binary operators are

 

written between the two operands.Binary operators are also used

 

with numeric operators.

Ternary operators

Ternary operators have three operands. C# supports only one

 

ternary operator, ? :.The ? : operator is equivalent to an if-else

 

statement.

 

 

Now look at the syntax of the ? : operator.

condition ? true value : false value

Here, condition is the condition of the if-else statement. It is a Boolean expression. If the condition evaluates to true, the true value is returned. Otherwise, the false value is returned.

The unary, binary, and ternary operators are further classified according to the operations they perform. Look at the operators supported by C# in detail.

The arithmetic operators are used to perform arithmetic operations. C# supports +, -, *, /, and %. These operators are similar to the operators in C++.

The increment and decrement operator increases or decreases the value of a variable by one. The increment operator is ++, and the decrement operator is --.

38

Part I

INTRODUCTION TO C#

 

 

 

The assignment operator performs an arithmetic operation and assigns the result to a variable.The commonly used assignment operators are =, +=, -=, *=, /=, %=, &=, |=, <<=, >>=, and ^=.

The logical operators supported by C# are &, |, ^, ~, &&, ||, true, false, and !.

The relational operators are also called comparison operators. These operators are used to compare two numeric values. An example of relational operators are ==,

<, >, <=, >=, and !=.

 

 

 

Y

 

 

 

L

The bit shifting operators are << and >>. These operators are used to shift the bit

to the left and right respectively.

F

 

 

 

 

M

 

The conditional operator is the ternary operator ? :, as discussed in the preceding

table. The ternary operator ? : is used for the if-else construct.

 

A

 

 

Each type of variable in C# has a specific range.If an operation results in the overflow of the range, C#Eprovides checked and unchecked operators. These operators are used to manageTthe overflow situation. When an operation is performed on the variable marked as checked, CLR (common language runtime) checks for overflow. If the value overflows, C# throws an exception. If you do not want an overflow check, mark the variable as unchecked. No exception is raised in this case even if an overflow occurs and may result in the loss of data.

You can use the is operator in C# to match the object with the type specified. You use the is and typeof operators to know the type of objects at run time.

C# provides the sizeof operator to find the size of the variable in bytes. The variable whose size is to be found is passed as a parameter to the sizeof operator.

Operator Overloading

C++ programmers are familiar with the concept of operator overloading. As seen earlier, all operators in C# perform a specified set of operations. However, if you want user-defined implementations, you use operator overloading. Many of the available operators in C# can be overloaded. For operator overloading, it is essential that one or both operands in the expression are of the struct type or a userdefined class. To declare an operator overload, use the operator keyword. The syntax of an operator overload is:

operator operator1

Team-Fly®