“C # Nature” study notes (f) operator
|
FREE Diabetes Recipes eBook! Click here to redeem. |
Operator usually divided into three categories: unary, binary operators, the ternary operator. Their corresponding operands are 1, 2 and 3.
Unary operator used to specify a negative value (decimal a =-2234234234234234.72M)
Binary operators require two operands are called the left operand and right operand. Binary operator code also requires the assignment of results to avoid losing the results of value. Binary operator into plus (+), subtraction (-), multiplication (*), divide (/), and remainder (%, sometimes referred to as the modulus operator.)
Binary operator precedence is as follows:
1 ).*,/ and% 2). + And – 3) .=
If the binary operator of the addition operator for strings, the string will play a role in splicing.
The use of characters in the arithmetic
While the char type is stored in the character and not numbers, but it is an integer type. It can join with other integer types of arithmetic operations. However, how to explain the value of type char, not based on stored in one of the characters, but on the basis of its value. For example, the number 3 contains a Unicode value of 0 × 33 (hex), converted to decimal value is 51. Figure 4 contains the Unicode value of 0 × 34, or decimal 52.
Cause unexpected floating-point types are not equal
Floating-point types float and double have some specificity, such as the way they handle precision.
A float with a 7-bit accuracy, which can hold values of 1234567 and 0.1234567. However, if the float value of the two added together, the results will be rounded up to 1234567, since the fractional part of a float can hold more than 7 digits. This type of rounding is sometimes fatal, especially in repetitive calculations or to check the time for equality.
In comparing the two values are equal, when the inaccuracy of floating point types can cause very serious consequences. Sometimes, the value should have been equal wrongly judged as not equal.
There are other special floating-point type. For example, an integer divide by zero, in theory, should result in an error. For the exact data type, this point is established. Then, float and double to allow some special value. Floating-point divide by zero, indicating “not a number”
float n = 0f; System.Console.WriteLine (n / 0); Output: non-numeric
Parentheses operator
Parentheses operator to allow combination of operands and operators, so that together they can find value. This is important because it provides a way beyond the default priority. Developers should take the initiative to use parentheses to enhance readability of the code to show the expression to eliminate the ambiguity, rather than relying on operator precedence.
Increment and decrement operators
C # provides special operators to implement counter increment and decrement. Increment operator (+ +) incremented by 1 each time to make a variable. Decrement operator (-) reduced by 1 each time to make a variable.
The contrast increment operator prefix and suffix
class IncrementExample
{
public static void Main ()
{
int x;
x = 1;
System.Console.WriteLine (“{0}, {1}, {2}”, x + +, x + +, x);
System.Console.WriteLine (“{0}. {1}, {2 }”,++ x, + + x, x);
}
}
Output:
1,2,3
4,5,5
It can be seen, the prefix increment / decrement operator returns the number of operating the up / down after the results, and the suffix increment / decrement operator returns the number of operating the up / down before the results.
Thread-safe increment and decrement
Used by System.Threading.Interlocked class provides thread-safe method Increment () and Decrement (). Both methods rely on the function of the processor to perform fast, thread-safe increment and decrement.
Constant expression
By definition, a constant expression is the C # compiler can be evaluated at compile time expression.
const keyword will lock the value at compile time. After the lock code, the value of any amendment or attempt will cause a compile error.
public Long Main ()
{
const int a = 60 * 60 * 24; / / constant expression
const int b = a * 7;
}
Note that the value assigned to b is a constant expression, because the operand expression is constant, so the compiler can determine the outcome.
Logical operators
Logical operators have one or two Boolean operands and returns a Boolean result. Logical operators allow you to combine multiple Boolean expressions to form other Boolean expressions. Logical operators, including ||,&& and ^, corresponding to OR, AND, XOR.
1.OR operator (| |)
| | Operators on two Boolean expressions are evaluated, and if any one is true, it returns true
2.AND operator (& &)
Boolean AND operator & & require both operands are evaluated only under the precondition for the true return true. Any operand is false, returns false.
3.XOR operator (^)
^ Symbol is the XOR operator. If applied to two Boolean operands, only a few in only one of the two operating under the premise is true, XOR operator will return true.
With the Boolean AND and OR is different from the Boolean XOR operator does not support short-circuit operations. It always have to check the two operands, because unless know exactly the value of both operands, or can not determine the final result.
Logical negation operator
Logical negation operator (!) Sometimes called NOT operator, its role is reversed value of a bool data type, which is a unary operator, only one operand.
Conditional operator
You can use conditional operator instead of if statements. The conditional operator is a question mark (?), The general format is as follows:
a? 1:2;
The conditional operator is ternary operator, because it requires three operands: a, 1,2.
If a request is true, then the conditional operator returns 1, otherwise 2.
Sorry, the comment form is closed at this time.


Comments
No comments yet.