csharp4 operators and_casts
TRANSCRIPT
Abed El-Azeem Bukhari (MCPD,MCTS and MCP)el-bukhari.com
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
Operators
Operators cont
Operators cont
operator shortcuts
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");}
Conditional operator
condition ? true_value: false_value
int x = 1;string s = x + " ";s += (x == 1 ? "man": "men");Console.WriteLine(s);
The checked and unchecked operatorsbyte b = 255;b++;Console.WriteLine(b.ToString());
-------------------------------------------------------byte b = 255;checked{b++;}Console.WriteLine(b.ToString());
The checked and unchecked operators contbyte b = 255;unchecked{b++;}Console.WriteLine(b.ToString());
The is operatorint 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 totrue in this case, and the appropriate message will be displayed.
The as operatorobject o1 = "Some String";object o2 = 5;string s1 = o1 as string; // s1 = "Some String"string s2 = o2 as string; // s2 = null
The sizeof operatorConsole.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 blockthe code within an unsafe block as illustrated here:unsafe{Console.WriteLine(sizeof(Customer));}
The typeof operatorThe 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.
nullable Types and operatorsint? a = null;int? b = a + 4; // b = nullint? c = a * 5; // c = null
int? a = null;int? b = -5;if (a > = b)Console.WriteLine("a > = b");elseConsole.WriteLine("a < b");//However, when comparing nullable types, if only one of the operands is null , the comparison will alwaysequate to false
The null Coalescing operator-If the first operand is not null , then the overall expression has the value of the first operand.
-If the first operand is null , then the overall expression has the value of the second operand.
For example:int? a = null;int b;b = a ?? 10; // b has the value 10a = 3;b = a ?? 10; // b has the value 3
operator Precedence
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
implicit Conversions
byte value1 = 10;
byte value2 = 23;
long total; // this will compile fine
total = value1 + value2;
Console.WriteLine(total);
implicit Conversions cont
implicit Conversions cont
-Nullable types implicitly convert to other nullable types following the conversion rules described for non-nullable types in the previous table; that is, int? implicitly converts to long?, float?, double?, and decimal?.
-Non-nullable types implicitly convert to nullable types according to the conversion rules described in the preceding table; that is, int implicitly converts to long?, float?, double?, and decimal?.
- Nullable types do not implicitly convert to non-nullable types; you must perform an explicitconversion as described in the next section. This is because there is the chance a nullable type will have the value null, which cannot be represented by a non-nullable type.
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.
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.
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.
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
boxing and unboxing
int myIntNumber = 20;object myObject = myIntNumber; // Box the intint mySecondNumber = (int)myObject; // Unbox it back into an int
Comparing objects for equality
-The ReferenceEquals() Method-The virtual Equals() Method-The static Equals() Method-Comparison operator (==)
The ReferenceEquals() Method
SomeClass x, y;x = new SomeClass();y = new SomeClass();bool B1 = ReferenceEquals(null, null); // returns truebool B2 = ReferenceEquals(null,x); // returns falsebool B3 = ReferenceEquals(x, y); // returns false because x and y// point to different objects
The virtual Equals() Method
This method used usually by override it in your classes in order toCompare the instances of your class.
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.
Comparison operator (==)
bool b = (x == y); // x, y object references
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;}
operator overloading cont
vect1 = new Vector(3.0, 3.0, 1.0);vect2 = new Vector(2.0, - 4.0, - 4.0);vect3 = vect1 + vect2;
//resultsvect1 = ( 3, 3, 1 )vect2 = ( 2, - 4, - 4 )vect3 = ( 5, - 1, - 3 )
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 equalitylogic as the == operator.
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;elsereturn false;}public static bool operator != (Vector lhs, Vector rhs){return ! (lhs == rhs);}
Which operators Can you overload?
user-defined Castsint I = 3;long l = I; // implicitshort s = (short)I; // explicitFor the predefined data types, explicit casts are required where there is a risk that the cast might fail or some data might be lost. The following are some examples:
-When converting from an int to a short, the short might not be large enough to hold the Value of the int.
-When converting from signed to unsigned data types, incorrect results will be returned if the signed variable holds a negative value.
- When converting from floating-point to integer data types, the fractional part of the numberwill be lost.
-When converting from a nullable type to a non-nullable type, a value of null will cause an exception.
implementing user-defined Castsstruct 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);}}
implementing user-defined Casts cont
Currency balance = new Currency(10,50);float f = balance; // We want f to be set to 10.5To 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);}
Casts Between Classes
public static explicit operator D(C value){// and so on}public static explicit operator C(D value){// and so on}
Casts Between Base and Derived ClassesMyBase derivedObject = new MyDerived();MyBase baseObject = new MyBase();MyDerived derivedCopy1 = (MyDerived) derivedObject; // OKMyDerived derivedCopy2 = (MyDerived) baseObject; // Throws exception
class DerivedClass: BaseClass{public DerivedClass(BaseClass rhs){// initialize object from the Base instance}// etc.
Thanks For Attending
Abed El-Azeem Bukhari (MCPD,MCTS and MCP)
el-bukhari.com