Upcoming SlideShare
×

# Operators & Casts

572 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
572
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
7
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Referhttp://msdn.microsoft.com/en-us/library/xhbhezf4(v=VS.100).aspxhttp://msdn.microsoft.com/en-us/library/aa691303(v=VS.71).aspx
• ### Operators & Casts

2. 2. OperatorsCATEGORY (BY OPERATOR(S) ASSOCIATIVITYPRECEDENCE) x.y f(x) a[x] x++ x-- new() typeof ()Primary left default checked unchecked delegateUnary + - ! ~ ++x --x (T)x leftMultiplicative * / % leftAdditive + - leftShift << >> leftRelational < > <= >= is as leftEquality == != rightLogical AND & leftLogical XOR ^ leftLogical OR | leftConditional AND && leftConditional OR || leftNull Coalescing ?? leftTernary ?: rightAssignment = *= /= %= += -= <<= >>= &= ^= |= => right
3. 3. Conditional Operator The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.  condition ? first_expression : second_expression; The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result.
4. 4. The Checked and UncheckedOperator The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions.  Ex: byte b=255; b+=5; //What is the value of is //The value of b here b?4By default, these non-constant expressions are notchecked for overflow at run time either, and they do notraise overflow exceptions. If you mark a block of code as checked, the CLR willenforce overflow checking, and throw OverflowExceptionif any overflow occurs.If you want to suppress overflow checking, you canmark the code as unchecked
5. 5. New Operators in C# The is Operator  Checks if an object is compatible with a given type. The as Operator  The as operator is used to perform certain types of conversions between compatible reference types. Null Coalescing Operator(??)  The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types.
6. 6. Type Conversion Type Safety  C# is a Strongly Typed language, this means that datatypes are not always seamlessly interchangeable. Type Conversion  Converting from one type to another type is called type conversion.  Implicit Conversion  No special syntax is required because the conversion is type safe and no data will be lost.  Explicit Conversion  Explicit conversions require a cast operator. Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons.
7. 7. Boxing Boxing  Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type.  When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object.  Ex: int i = 123; // The following line boxes i. object o = i;
8. 8. Unboxing Unboxing  Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface.  The following statements demonstrate both boxing and unboxing operations int i = 123; // a value type object o = i; // boxing int j = (int)o; // unboxing
9. 9. Comparing Objects for Equality The mechanisms of object equality are different depending on whether you are comparing Reference types (instances of classes) or Value types (primitive types or instances of structs). System.Object defines three different methods for comparing objects.  ReferenceEquals(ref1,ref2)  It is a static method that tests whether two references refer to the same instance of a class.  The Virtual Equals(Object)  It is also for comparing references, however you can override it in your own class because it is virtual
10. 10. Comparing Objects for Equality The static Equals(Object, Object)  Determines whether the specified object instances are considered equal. Comparison Operator (==)  For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise.  For reference types other than string, == returns true if its two operands refer to the same object.  For the string type, == compares the values of the strings.