Core C# and .NET
< Day Day Up > |
The C# operators used for arithmetic operations, bit manipulation, and conditional program flow should be familiar to all programmers. This section presents an overview of these operators that is meant to serve as a syntactical reference. Arithmetic Operators
Table 2-4 summarizes the basic numerical operators. The precedence in which these operators are applied during the evaluation of an expression is shown in parentheses, with 1 being the highest precedence.
Core Note
C# does not provide an exponentiation operator. Instead, use the Math.Pow() method to raise a number to a power, and Math.Exp() to raise e to a power.
Conditional and Relational Operators
Relational operators are used to compare two values and determine their relationship. They are generally used in conjunction with conditional operators to form more complex decision constructs. Table 2-5 provides a summary of C# relational and conditional operators.
Note the two forms of the logical AND/OR operations. The && and || operators do not evaluate the second expression if the first is false a technique known as short circuit evaluation. The & and | operators always evaluate both expressions. They are used primarily when the expression values are returned from a method and you want to ensure that the methods are called. In addition to the operators in Table 2-5, C# supports a ?: operator for conditionally assigning a value to a variable. As this example shows, it is basically shorthand for using an if-else statement: string pass; int grade=74; If(grade >= 70) pass="pass"; else pass="fail"; // expression ? op1 : op2 pass = (grade >= 70) ? "pass" : "fail"; If the expression is TRue, the ?: operator returns the first value; if it's false, the second is returned. Control Flow Statements
The C# language provides if and switch conditional constructs that should be quite familiar to C++ and Java programmers. Table 2-6 provides a summary of these statements.
if-else
Syntax: if ( boolean expression ) statement if ( boolean expression ) statement1 else statement2 C# if statements behave as they do in other languages. The only issue you may encounter is how to format the statements when nesting multiple if-else clauses. // Nested if statements if (age > 16) if (age > 16) { if (sex == "M") if (sex == "M") type = "Man"; { else type = "Man"; type = "Woman" ; } else { else type = "Woman" ; type = "child"; } } else { type = "child"; } Both code segments are equivalent. The right-hand form takes advantage of the fact that curly braces are not required to surround single statements; and the subordinate if clause is regarded as a single statement, despite the fact that it takes several lines. The actual coding style selected is not as important as agreeing on a single style to be used. switch
Syntax: switch( expression ) {switch block} The expression is one of the int types, a character, or a string. The switch block consists of case labels and an optional default label associated with a constant expression that must implicitly convert to the same type as the expression. Here is an example using a string expression: // switch with string expression using System; public class MyApp { static void Main(String[] args) { switch (args[0]) { case "COTTON": // is case sensitive case "cotton": Console.WriteLine("A good natural fiber."); goto case "natural"; case "polyester": Console.WriteLine("A no-iron synthetic fiber."); break; case "natural": Console.WriteLine("A Natural Fiber. "); break; default: Console.WriteLine("Fiber is unknown."); break; } } }
The most important things to observe in this example are as follows:
|
< Day Day Up > |