Praise for C# 2.0: Practical Guide for Programmers 2005 phần 5 pptx

26 378 1
Praise for C# 2.0: Practical Guide for Programmers 2005 phần 5 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

■ 5.2 Assignment Operators 85 The destination Variable and the source Expression must be type compatible. The Variable can store either a simple data value or an object reference. Assignments of Simple Values Examples: int a, b, c; a=1; //OK. b=a; //OK. a = c; // Error: Variable must be initialized before used. 1 = a; // Error: Destination must be a variable. c = (a + b); // OK. (a + b) = c; // Error: Destination must be a variable. The assignment operator has the lowest precedence, allowing the expression on the right- hand side to be evaluated before any assignment: int a; a=1; System.Console.WriteLine(a); a=a-1; //-hashigher precedence than = System.Console.WriteLine(a); a=2+a*3; //(2+(0*3)) System.Console.WriteLine(a); Output: 1 0 2 Assignments of References Examples: Id id1 = new Id("Frank", 1); Id id2 = new Id("Emma", 2); id2 = id1; Copying references by assignment does not copy the content of the source object, only its reference. Now id2 refers to the same Id object as id1, and the previous object Id("Emma", 2) is eligible for garbage collection. 86 Chapter 5: Operators, Assignments, and Expressions ■ 5.2.2 Multiple Assignments An assignment always returns the value of the expression on the right-hand side as a result. Therefore, initializing several variables to a common value or reference using multiple assignment statements: int a, b, c; a=1;b=1;c=1; can be reduced to a single statement using multiple assignments as shown: a=b=c=1; //a=(b=(c=1)); The preceding example illustrates the importance of right associativity for the assignment operator. 5.3 Conditional Operator The conditional operator evaluates a boolean expression, and, depending on its resultant value (either true or false), executes one of two expressions as defined here: EBNF ConditionalOperator = Condition "?" ExprIfConditionTrue ":" ExprIfConditionFalse. The conditional operator, therefore, is equivalent to a simple if-else statement: if ( Condition ) ExprIfConditionTrue else ExprIfConditionFalse For example: minimum=a<b?a:b; is equivalent to: if (a < b) minimum = a; else minimum = b; Another example: absolute=a<0?-a:a; ■ 5.4 Null Coalescing Operator 87 is equivalent to: if (a < 0) absolute = -a; else absolute = a; This operator can be nested but becomes rapidly unreadable: a?b?c:d:e // Evaluates as (a?(b?c:d):e) 5.4 Null Coalescing Operator Given that a represents an operand of a nullable or reference type, the null coalescing operator is defined as in Table 5.2. Name Notation Meaning Null Coalescing a??b Returns a if a is non-null, otherwise returns b. Table 5.2: Null coalescing operator. Example: using System; public class CoalescingNull { public static void Main(string[] args) { bool? a = true; bool? b = false; object o = null; Console.WriteLine("|{0,4}|{1,4}|{2,4}|{3,4}|", a??b, a??o, o??b, o??o ); } } Output: |True|True|False| | 88 Chapter 5: Operators, Assignments, and Expressions ■ 5.5 Conditional Logical Operators Given that a and b represent boolean expressions, the conditional logical operators are defined as in Table 5.3. Name Notation Meaning Conditional Logical AND a && b true if both operands are true, otherwise false Conditional Logical OR a || b true if either or both operands are true, otherwise false Conditional Logical NOT !a true if the operand is false, otherwise false Table 5.3: Conditional logical operators. These operators are much like logical (bitwise) operators except that their evaluation is short-circuited. The short-circuit eliminates unnecessary evaluations. If the value of a is false then the expression a&&balso evaluates to false without the need to evaluate b. Similarly, if the value of a is true then the expression a||balso evaluates to true without the need to evaluate b. Table 5.4 summarizes all combinations of boolean values. a b a&&b a||b true true true true true false false true false true false true false false false false Table 5.4: Values for conditional logical operators. Example: using System; public class ConditionalLogical { public static void Main(string[] args) { bool t, b, f = false; t=b=true; Console.WriteLine( (t && t) +" "+ (t && f) +" "+ (f && t) +" "+ (f && f) ); Console.WriteLine( (t || t) +" "+ (t || f) +" "+ (f || t) +" "+ (f || f) ); ■ 5.6 Logical Operators 89 // t&&b where f not evaluated (short-circuited) Console.Write("{0} ", t&&b || f ); // f || b where t not evaluated (short-circuited) Console.Write("{0} ", f&&t || b ); // f || f where b not evaluated (short-circuited) Console.Write("{0} ", f || f&&b ); // t&&b where f not evaluated (short-circuited) Console.WriteLine("{0}", f || t&&b ); } } Output: True False False False True True True False True True False True 5.6 Logical Operators Given that a and b are corresponding bit values in the left-hand and right-hand operands, respectively, the logical (bitwise) operators are defined as in Table 5.5. Name Notation Meaning Logical NOT ˜a Invert the bit value (Complement) Logical AND a&b 1 if both bits are 1, otherwise 0 Logical OR a|b 1 if either or both bits are 1, otherwise 0 Logical XOR aˆb 1 if and only if one of the bits is 1, otherwise 0 Table 5.5: Logical operators. a b ˜a a&b a|b aˆb 11 0 1 1 0 10 0 0 1 1 01 1 0 1 1 001000 Table 5.6: Values for logical operators. 90 Chapter 5: Operators, Assignments, and Expressions ■ Valid types for logical operators are integers and boolean. Example: public class LogicalBitwise { public static void Main(string[] args) { ushort a = 0x005A; // in binary = 0000 0000 0101 1010 ushort b = 0x3C5A; // in binary = 0011 1100 0101 1010 System.Console.WriteLine( "{0:x}", a&b); System.Console.WriteLine( "{0:x}", a|b); System.Console.WriteLine( "{0:x}", aˆb); System.Console.WriteLine( "{0:x}", ˜a ); System.Console.WriteLine( "{0:x}", ˜b ); } } Output: 5a 3c5a 3c00 ffffffa5 ffffc3a5 5.6.1 Logical Operators as Conditional Logical Operators The logical bitwise operators may also be used as conditional logical operators since they can be applied to boolean operands and return a bool value. Given that a and b represent boolean expressions, the logical operators are defined as in Table 5.7. Name Notation Meaning Logical NOT !a Returns the complement of the truth value of a (negation) Logical AND a & b true if both operands are true, otherwise false Logical OR a | b true if either or both operands are true, otherwise false Logical XOR a ˆ b true if and only if one operand is true, otherwise false Table 5.7: Logical operators as conditional logical operators. Note that the logical operators & and | have the same truth values as their corresponding conditional operators, && and || (Table 5.8). ■ 5.6 Logical Operators 91 a b !a a&b a|b aˆb true true false true true false true false false false true true false true true false true true false false true false false false Table 5.8: Values for logical operators. 5.6.2 Compound Logical Assignment Operators Given that a and b represent integral expressions, the compound logical assignment operators are defined as in Table 5.9. Name Notation Meaning Compound Logical AND b &= a b = (Type) (b & (a)) Compound Logical OR b |= a b = (Type) (b | (a)) Compound Logical XOR b ˆ= a b = (Type) (b ˆ (a)) Table 5.9: Compound logical assignment operators. Based on the preceding table, a and b are first promoted to int and the result is implicitly narrowed to the destination (Type)ofb upon assignment: byte b = 0x62; // 0x62 b &= 0x0F; // 0x62 & 0x0F => 0x00000002 (now an int) // 0x02 (cast back to a byte) Example: using System; public class CompoundLogical { public static void Main(string[] args) { bool t, b, f = false; t=b=true; Console.WriteLine( (t & t) +" "+ (t & f) +" "+ (f & t) +" "+ (f&f)); Console.WriteLine( (t | t) +" "+ (t | f) +" "+ (f | t) +" "+ (f|f)); 92 Chapter 5: Operators, Assignments, and Expressions ■ // t&b where f not evaluated (short-circuited) Console.Write("{0} ", t&b | f); // f | b where t not evaluated (short-circuited) Console.Write("{0} ", f&t | b); //f|f where b not evaluated (short-circuited) Console.Write("{0} ", f | f&b); // t&b where f not evaluated (short-circuited) Console.Write("{0} ", f | t&b); Console.Write("{0} ", f &= t | t); // (1) f=(f&(t|t)) Console.WriteLine("{0}",f=f&t|t);//(2)f=((f&t)|t) } } Output: True False False False True True True False True True False True False True Note that assignments (1) and (2) give different results for the same value of the operands. 5.7 Equality Operators 5.7.1 Simple Value Type Equality Given that a and b represent operands of simple value types, the simple-type equality operators are defined as in Table 5.10. Name Notation Meaning Equal a == b true if a and b have the same simple value, otherwise false Not Equal a != b true if a and b have different simple values, otherwise false Table 5.10: Simple value type equality operators. Example: public class Equality { public static void Main(string[] args) { System.Console.WriteLine( 9 == 9 ); System.Console.WriteLine( 0 != 1 ); ■ 5.7 Equality Operators 93 System.Console.WriteLine( ‘9’ == 9 ); // Operands are promoted // as ints. System.Console.WriteLine( ‘0’ == 0x30 ); // Operands are promoted // as ints. } } Output: True True False True When comparing floating-point numbers, bear in mind that values are approximated to a finite number of bits. 5.7.2 Object Reference and Value Equality The predefined reference-type equality operators == and != accept operands of type object. Testing two object references o1 and o2 using Object.ReferenceEquals(o1, o2) is equivalent to: (object)o1 == (object)o2 Examples: bool b; Name m1 = new Name("Michel"); Name m2 = new Name("Michel"); string m3 = "Michel"; b = m2 == m3; // Compile-time error (incompatible type). b = m1 == m2; // False (two different objects). m1 = m2; // Alias to the same object m2. b = m1 == m2; // True (same reference). The null reference can be assigned and compared to any object reference. The Object.Equals method also returns true only if the object is compared to itself: class Object { bool Equals(Object o) { return o == this; } } However, it is common practice to overload the equality operators and to override the Tip Equals method in derived classes to provide object value rather than object reference equality as shown in Section 4.6. 94 Chapter 5: Operators, Assignments, and Expressions ■ 5.8 Relational Operators Given that a and b represent numeric expressions, the relational operators are defined as in Table 5.11. Name Notation Meaning Less Than a < b true if a is less than b, otherwise false Less Than or Equal a <= b true if a is less than or equal to b, otherwise false Greater Than a > b true if a is greater than b, otherwise false Greater Than or Equal a >= b true if a is greater than or equal to b, otherwise false Table 5.11: Relational operators. All relational operators are binary operators and all operands are promoted, if necessary, to numeric values. The evaluation results in a boolean value. Relational operators have a lower precedence than arithmetic operators, but a higher precedence than the assignment operators. Example: public class Relational { public static void Main(string[] args) { System.Console.WriteLine( 1 < 2 ); System.Console.WriteLine( 3 <= 4 ); System.Console.WriteLine( 5 > 6 ); System.Console.WriteLine( 7 >= 8 ); System.Console.WriteLine( 5.5 >= 5.0 ); System.Console.WriteLine( ‘A’ < ‘a’ ); // Operands are promoted // as ints. } } Output: True True False False True True [...]... modulus operator returns the remainder of a division performed on either floating-point or integral operands Examples: 13 % 5 11 .5 % 2 .5 10 / 0 4.0 / 5 4.0 / 0.0 -4.0 / 0.0 0.0 / 0.0 // // // // // // // 3 1 .5 DivideByZeroException 0.8 Infinity (positive infinity) -Infinity (negative infinity) NaN (not-a-number) Because C# uses the IEEE 754 formats for floating-point types, dividing a non-zero number by... + 13.0 13 11 .5 5 + 5 5 - 2 .5 // // // // 18 18.0 8 9.0 The binary operator + also acts as a string concatenation if one or both of the operands is a string object If only one of the operands is a string, the other is implicitly converted to its string representation method before the concatenation is performed For non-string ■ 5. 10 Arithmetic Operators 99 objects, this conversion is performed using... presented in Sections 5. 5 and 5. 6, respectively 5. 11.1 Prefix and Postfix Operators The increment (++) and decrement ( ) operators come in two flavors: prefix and postfix In the prefix case, the increment or decrement operator is placed before a simple data type variable For example: ++a // a = a + 1 In the postfix case, the increment or decrement operator is placed after a simple data type variable For example: a... ++a); // 1 + 3 = 4 ++a + a++); // 3 + 1 = 4 } } Output: 3 2 3 4 4 4 5. 11.2 Explicit Casts C# is a strongly typed language and therefore checks for type compatibility at compiletime As seen in Section 5. 8.1, some checks are only possible at runtime using the is and as operators An operator, however, can have incompatible operands, for example, assigning a char to a byte In that case, a cast must be... f fffffffe(-2) ffffffe8(-24) 5. 10 Arithmetic Operators 5. 10.1 Multiplicative Operators Given that a and b represent operands of numeric data types, the multiplicative operators are defined as in Table 5. 14 Name Multiplication Division Modulus Notation a * b a / b a % b Table 5. 14: Binary multiplicative operators Meaning a multiplied by b a divided by b a mod b 98 Chapter 5: Operators, Assignments, and...■ 5. 8.1 5. 8 Relational Operators 95 Type Testing Two additional operators in C# are used for type testing: is and as The is operator defined next returns a boolean value and is used to determine at runtime if an object is an instance of Type or any... -Infinity as a result Similarly, dividing zero by zero generates NaN as a result 5. 10.2 Additive Operators Given that a and b represent operands of numeric data types, the additive operators are defined as in Table 5. 15 Name Notation Addition Subtraction a + b a - b Meaning a is added to b b is subtracted from a Table 5. 15: Binary additive operators If either a or b is a floating-point value, then the... right) Table 5. 18: Method notations for overloadable operators Exercises Exercise 5- 1 Improve class Id by adding the overloaded operators == and != Exercise 5- 2 Write similar TypeTesting classes that create Person and Id objects and pass them as parameters to a static method Print(Id id) in order to identify objects at runtime using type-testing operators chapter 6 Statements and Exceptions I n C#, statements... innermost block in which it is declared For example: { for (int n = 0; n < 8; n++) { // n is in the scope of the for } char c; // Declaration closer to its related code } ■ 6.3 Embedded Statements 109 Finally, any variable that is used before declaration or is accessed outside its scope generates a compilation error 6.3 Embedded Statements Embedded statements in C# include many of the well-known constructs... are very useful for updating variables in loops where only the sideeffect of the operator is of interest Even if both are considered unary operators, postfix operators are in the primary category and therefore evaluated before the prefix operators The following example illustrates the precedence of the ++ postfix operator over its prefix one: class TestPrePost { public static void Main() { ■ 5. 12 Other Primary . Now id2 refers to the same Id object as id1, and the previous object Id("Emma", 2) is eligible for garbage collection. 86 Chapter 5: Operators, Assignments, and Expressions ■ 5 .2. 2 Multiple. aˆb); System.Console.WriteLine( "{0:x}", ˜a ); System.Console.WriteLine( "{0:x}", ˜b ); } } Output: 5a 3c5a 3c00 ffffffa5 ffffc3a5 5. 6.1 Logical Operators as Conditional Logical. the remainder of a division performed on either floating-point or integral operands. Examples: 13 % 5 // 3 11 .5 % 2. 5 // 1 .5 10 / 0 // DivideByZeroException 4.0 / 5 // 0.8 4.0 / 0.0 // Infinity

Ngày đăng: 05/08/2014, 10:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan