Você está na página 1de 4

Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

Lecture (6 Nov)
Operators

Downloaded from: www.onspot.pk

Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

The Conditional Operator The conditional operator ( ?: ), also known as the ternary operator, is a shorthand form of the if...else construction. int x = 1; string s = x + ; s += (x == 1 ? man : men); Console.WriteLine(s); The checked and unchecked Operators Consider the following code:
byte b = 255; b++; Console.WriteLine(b.ToString());

The byte data type can hold values only in the range zero to 255, so incrementing the value of b causes an overflow.

byte b = 255; checked { b++; } Console.WriteLine(b.ToString());

When you try to run this code, you will get an error message like this: Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow. at Wrox.ProCSharp.Basics.OverflowTest.Main(String[] args) You can enforce overflow checking for all unmarked code in your program by specifying the /checked compiler option. If you want to suppress overflow checking, you can mark the code as unchecked :
byte b = 255; unchecked { b++; } Console.WriteLine(b.ToString());

In this case, no exception will be raised, but you will lose data because the byte type cannot hold a value of 256, the overflowing bits will be discarded, and your b variable will hold a value of zero (0).

Downloaded from: www.onspot.pk

Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

Note that unchecked is the default behavior. The only time you are likely to need to explicitly use the unchecked keyword is if you need a few unchecked lines of code inside a larger block that you have explicitly marked as checked .

The is Operator The is operator allows you to check whether an object is compatible with a specific type. The phrase is compatible means that an object either is of that type or is derived from that type. For example, to check whether a variable is compatible with the object type, you could use the following bit of code: int i = 10; if (i is object) { Console.WriteLine(i is an object); } int , like all C# data types, inherits from object ; therefore the expression i is object will evaluate to true in this case, and the appropriate message will be displayed. The as Operator The as operator is used to perform explicit type conversions of reference types. If the type being converted is compatible with the specified type, conversion is performed successfully. However, if the types are incompatible, the as operator returns the value null . As shown in the following code, attempting to convert an object reference to a string will return null if the object reference does not actually refer to a string instance:
object o1 = Some String; object o2 = 5; string s1 = o1 as string; // s1 = Some String string s2 = o2 as string; // s2 = null

The as operator allows you to perform a safe type conversion in a single step without the need to first test the type using the is operator and then perform the conversion. The sizeof Operator You can determine the size (in bytes) required on the stack by a value type using the sizeof operator:
unsafe { Console.WriteLine(sizeof(int)); }

This will display the number 4 , because an int is 4 bytes long. Notice that you can use the sizeof operator only in unsafe code. Chapter 12 , Memory Management and Pointers, looks at unsafe code in more detail.

Downloaded from: www.onspot.pk

Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

The typeof Operator The typeof operator returns a System.Type object representing a specified type. For example, typeof(string) will return a Type object representing the System.String type. This is useful when you want to use reflection to find information about an object dynamically. Chapter 13 , Reflection, looks at reflection.

Downloaded from: www.onspot.pk

Você também pode gostar