Adv Oop Lect11

Embed Size (px)

Citation preview

  • 8/13/2019 Adv Oop Lect11

    1/4

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

    Lecture (6 Nov)Operators

    Downloaded from: www.onspot.pk

  • 8/13/2019 Adv Oop Lect11

    2/4

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

    The Condit ional OperatorThe conditional operator ( ?: ), also known as the ternary operator, is a shorthand formof 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 thevalue 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 anoverflow. at Wrox.ProCSharp.Basics.OverflowTest.Main(String[] args)

    You can enforce overflow checking for all unmarked code in your program by specifyingthe /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 typecannot hold a value of 256, the overflowing bits will be discarded, and your b variable willhold a value of zero (0).

    Downloaded from: www.onspot.pk

  • 8/13/2019 Adv Oop Lect11

    3/4

    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 toexplicitly use the unchecked keyword is if you need a few unchecked lines of code insidea 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 fromthat 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 OperatorThe as operator is used to perform explicit type conversions of reference types. If thetype being converted is compatible with the specified type, conversion is performedsuccessfully. However, if the types are incompatible, the as operator returns the valuenull . As shown in the following code, attempting to convert an object reference to astring 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 theneed to first test the type using the is operator and then perform the conversion.

    The sizeof OperatorYou can determine the size (in bytes) required on the stack by a value type using thesizeof 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 , MemoryManagement and Pointers, looks at unsafe code in more detail.

    Downloaded from: www.onspot.pk

  • 8/13/2019 Adv Oop Lect11

    4/4

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

    The typeof OperatorThe typeof operator returns a System.Type object representing a specified type. Forexample, 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 objectdynamically. Chapter 13 , Reflection, looks at reflection.

    Downloaded from: www.onspot.pk