Csharp4 operators and_casts

925 views
867 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
925
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
83
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Csharp4 operators and_casts

  1. 1. Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
  2. 2. Operators and Casts Prepared By : Abed ElAzeem Bukhari What ‘s in this chapter? ➤ Operators in C# ➤ The idea of equality when dealing with reference and value types ➤ Data conversion between primitive data types ➤ Converting value types to reference types using boxing ➤ Converting between reference types by casting ➤ Overloading the standard operators for custom types ➤ Adding cast operators to custom types
  3. 3. Operators
  4. 4. Operators cont
  5. 5. Operators cont
  6. 6. operator shortcuts
  7. 7. operator shortcuts cont int x = 5; if (++x == 6) // true – x is incremented to 6 before the evaluation { Console.WriteLine("This will execute"); } if (x++ == 7) // false – x is incremented to 7 after the evaluation { Console.WriteLine("This won't"); }
  8. 8. Conditional operator condition ? true_value: false_value int x = 1; string s = x + " "; s += (x == 1 ? "man": "men"); Console.WriteLine(s);
  9. 9. The checked and unchecked operators byte b = 255; b++; Console.WriteLine(b.ToString()); ------------------------------------------------------- byte b = 255; checked { b++; } Console.WriteLine(b.ToString());
  10. 10. The checked and unchecked operators cont byte b = 255; unchecked { b++; } Console.WriteLine(b.ToString());
  11. 11. The is operator 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 .
  12. 12. The as operator object o1 = "Some String"; object o2 = 5; string s1 = o1 as string; // s1 = "Some String" string s2 = o2 as string; // s2 = null
  13. 13. The sizeof operator Console.WriteLine(sizeof(int)); // This will display the number 4 , because an int is 4 bytes long. If you are using the sizeof operator with complex types (and not primitive types), you will need to block the code within an unsafe block as illustrated here: unsafe { Console.WriteLine(sizeof(Customer)); }
  14. 14. 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.
  15. 15. nullable Types and operators int? a = null; int? b = a + 4; // b = null int? c = a * 5; // c = null int? a = null; int? b = -5; if (a > = b) Console.WriteLine(&quot;a > = b&quot;); else Console.WriteLine(&quot;a < b&quot;); // However, when comparing nullable types, if only one of the operands is null , the comparison will always equate to false
  16. 16. The null Coalescing operator <ul><li>If the first operand is not null , then the overall expression has the value of the first operand. </li></ul><ul><li>If the first operand is null , then the overall expression has the value of the second operand. </li></ul><ul><li>For example: </li></ul><ul><li>int? a = null; </li></ul><ul><li>int b; </li></ul><ul><li>b = a ?? 10; // b has the value 10 </li></ul><ul><li>a = 3; </li></ul><ul><li>b = a ?? 10; // b has the value 3 </li></ul>
  17. 17. operator Precedence
  18. 18. Type Conversions byte value1 = 10; byte value2 = 23; byte total; total = value1 + value2; Console.WriteLine(total); When you attempt to compile these lines, you get the following error message: Cannot implicitly convert type ‘ int ’ to ‘ byte‘ The problem here is that when you add 2 bytes together, the result will be returned as an int , not as another byte
  19. 19. implicit Conversions byte value1 = 10; byte value2 = 23; long total; // this will compile fine total = value1 + value2; Console.WriteLine(total);
  20. 20. implicit Conversions cont
  21. 21. implicit Conversions cont <ul><li>Nullable types implicitly convert to other nullable types following the conversion rules </li></ul><ul><li>described for non-nullable types in the previous table; that is, int? implicitly converts </li></ul><ul><li>to long?, float?, double?, and decimal?. </li></ul><ul><li>Non-nullable types implicitly convert to nullable types according to the conversion rules </li></ul><ul><li>described in the preceding table; that is, int implicitly converts to long?, float?, double?, </li></ul><ul><li>and decimal?. </li></ul><ul><li>- Nullable types do not implicitly convert to non-nullable types; you must perform an explicit </li></ul><ul><li>conversion as described in the next section. This is because there is the chance a nullable </li></ul><ul><li>type will have the value null, which cannot be represented by a non-nullable type. </li></ul>
  22. 22. explicit Conversions These are some of the conversions that cannot be made implicitly: ➤ int to short — Data loss is possible. ➤ int to uint — Data loss is possible. ➤ uint to int — Data loss is possible. ➤ float to int — You will lose everything after the decimal point. ➤ Any numeric type to char — Data loss is possible. ➤ decimal to any numeric type — The decimal type is internally structured differently from both integers and floating-point numbers. ➤ int? to int — The nullable type may have the value null.
  23. 23. explicit Conversions These are some of the conversions that cannot be made implicitly: ➤ int to short — Data loss is possible. ➤ int to uint — Data loss is possible. ➤ uint to int — Data loss is possible. ➤ float to int — You will lose everything after the decimal point. ➤ Any numeric type to char — Data loss is possible. ➤ decimal to any numeric type — The decimal type is internally structured differently from both integers and floating-point numbers. ➤ int? to int — The nullable type may have the value null.
  24. 24. explicit Conversions cont long val = 30000; int i = (int)val; // A valid cast. The maximum int is 2147483647 long val = 3000000000; int i = (int)val; // An invalid cast. The maximum int is 2147483647 // I will be -1294967296 long val = 3000000000; int i = checked((int)val); // to check before casting Using casts, you can convert most primitive data types from one type to another; for example, in this code, the value 0.5 is added to price, and the total is cast to an int: double price = 25.30; int approximatePrice = (int)(price + 0.5); This gives the price rounded to the nearest dollar.
  25. 25. explicit Conversions cont ushort c = 43; char symbol = (char)c; Console.WriteLine(symbol); // The output is the character that has an ASCII number of 43, the + sign. --- int? a = null; int b = (int)a; // Will throw exception --- int i = 10; string s = i.ToString(); --- if you need to parse a string to retrieve a numeric or Boolean value, you can use the Parse() method supported by all the predefined value types: string s = “100”; int i = int.Parse(s) ; Console.WriteLine(i + 50); // Add 50 to prove it is really an int
  26. 26. boxing and unboxing int myIntNumber = 20; object myObject = myIntNumber; // Box the int int mySecondNumber = (int)myObject; // Unbox it back into an int
  27. 27. Comparing objects for equality <ul><li>The ReferenceEquals() Method </li></ul><ul><li>The virtual Equals() Method </li></ul><ul><li>The static Equals() Method </li></ul><ul><li>Comparison operator (==) </li></ul>
  28. 28. The ReferenceEquals () Method SomeClass x, y; x = new SomeClass(); y = new SomeClass(); bool B1 = ReferenceEquals(null, null); // returns true bool B2 = ReferenceEquals(null,x); // returns false bool B3 = ReferenceEquals(x, y); // returns false because x and y // point to different objects
  29. 29. The virtual Equals() Method This method used usually by override it in your classes in order to Compare the instances of your class.
  30. 30. The static Equals() Method The static version of Equals() actually does the same thing as the virtual instance version. The difference is that the static version takes two parameters and compares them for equality.
  31. 31. Comparison operator (==) bool b = (x == y); // x, y object references
  32. 32. operator overloading VectorStruct solution public static Vector operator + (Vector lhs, Vector rhs) { Vector result = new Vector(lhs); result.x += rhs.x; result.y += rhs.y; result.z += rhs.z; return result; }
  33. 33. operator overloading cont vect1 = new Vector(3.0, 3.0, 1.0); vect2 = new Vector(2.0, - 4.0, - 4.0); vect3 = vect1 + vect2; //results vect1 = ( 3, 3, 1 ) vect2 = ( 2, - 4, - 4 ) vect3 = ( 5, - 1, - 3 )
  34. 34. overloading the Comparison operators ➤ == and != ➤ > and < ➤ > = and < = The C# language requires that you overload these operators in pairs. That is, if you overload == , you must overload != too; otherwise, you get a compiler error. In addition, the comparison operators must return a bool . If you overload == and !=, you must also override the Equals() and GetHashCode() methods inherited from System.Object; otherwise, you’ll get a compiler warning. The reasoning is that the Equals() method should implement the same kind of equality logic as the == operator.
  35. 35. overloading the Comparison operators cont public static bool operator == (Vector lhs, Vector rhs) { if (lhs.x == rhs.x & & lhs.y == rhs.y & & lhs.z == rhs.z) return true; else return false; } public static bool operator != (Vector lhs, Vector rhs) { return ! (lhs == rhs); }
  36. 36. Which operators Can you overload?
  37. 37. user-defined Casts <ul><li>int I = 3; </li></ul><ul><li>long l = I; // implicit </li></ul><ul><li>short s = (short)I; // explicit </li></ul><ul><li>For the predefined data types, explicit casts are required where there is a risk that the cast </li></ul><ul><li>might fail or some data might be lost. The following are some examples: </li></ul><ul><li>When converting from an int to a short, the short might not be large enough to hold the </li></ul><ul><li>Value of the int. </li></ul><ul><li>When converting from signed to unsigned data types, incorrect results will be returned if </li></ul><ul><li>the signed variable holds a negative value. </li></ul><ul><li>- When converting from floating-point to integer data types, the fractional part of the number </li></ul><ul><li>will be lost. </li></ul><ul><li>When converting from a nullable type to a non-nullable type, a value of null will cause </li></ul><ul><li>an exception. </li></ul>
  38. 38. implementing user-defined Casts struct Currency { public uint Dollars; public ushort Cents; public Currency(uint dollars, ushort cents) { this.Dollars = dollars; this.Cents = cents; } public override string ToString() { return string.Format( “ ${0}.{1, - 2:00} ” , Dollars,Cents); } }
  39. 39. implementing user-defined Casts cont Currency balance = new Currency(10,50); float f = balance; // We want f to be set to 10.5 To be able to do this, you need to define a cast. Hence, you add the following to your Currency definition: public static implicit operator float (Currency value) { return value.Dollars + (value.Cents/100.0f); }
  40. 40. Casts Between Classes public static explicit operator D(C value) { // and so on } public static explicit operator C(D value) { // and so on }
  41. 41. Casts Between Base and Derived Classes MyBase derivedObject = new MyDerived(); MyBase baseObject = new MyBase(); MyDerived derivedCopy1 = (MyDerived) derivedObject; // OK MyDerived derivedCopy2 = (MyDerived) baseObject; // Throws exception class DerivedClass: BaseClass { public DerivedClass( BaseClass rhs) { // initialize object from the Base instance } // etc.
  42. 42. Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

×