Chapter 1 Programming of Visual C # Best Practices (iv): Operators

Source: Internet
Author: User
ArticleDirectory
    • 1.4.1 operator Classification
    • 1.4.2 operator priority
Chapter 1 Program Design

"To become a real programmer, we need a baptism ."
"Program = Data Structure +Algorithm." Such a formula is very incisive. It directly describes the nature of the program beyond the surface layer. In addition, such a few simple words also let us understand "what should we learn ?". People have programs to do everything, but they are not aware of them. Some people list their daily actions in a table, which is "Program writing ".

 

The focus of this chapter:

◆ Operator Classification
◆ Operator priority

 

1.4 Operator

An expression is composed of an operand (operand) and an operator (operator. The expression operator is used to indicate the operations performed on the operands. Common operators include +,-, *,/, and %. The operands are composed of variables and constants. Constants will be discussed later in this chapter. Here we will mainly introduce the knowledge of operators and hope to help you.

 

1.4.1 operator Classification

An operator is a very important part of an expression. It indicates the operations on the operands in the expression, such as +,-, *,/, and %. Operators can be divided into the following three categories based on the number of operator operations:
Unary operator: contains only one operand and uses prefix notation (for example, -- X) or suffix notation (for example, X ++ ).
Binary OPERATOR: it has two operands and all use the infix notation (for example, X + Y ).
Ternary OPERATOR: contains three operands and uses the infix notation (such? : Operator ). Note: the C # language only contains one ternary operator.
The following table lists common operators in C:
CATEGORY Operators
Arithmetic Operator +-*/%
Logical operators ~ & |!
String concatenation operator +
Increment and Decrement Operators ++ --
Shift Operator <>
Comparison operator =! = <> <=> =
Value assignment operator = + =-= * =/= % = <=^= <=> =
Member access operator.
Index operator []
Data type conversion operator ()
Conditional operator? :
Delegate join and delete operators +-
Object creation operator new
Type Information operator sizeof (used only for insecure Code ) Is typeof
Overflow exception control operator checked unchecked
Indirect addressing operator *-> & (used only for Insecure code) []
Namespace alias qualifier ::
Null join operator ??
! , ++ X, -- X, and (t) x are called unary operators. The (t) x operator is a forced conversion operator, which can convert one type to another. The following is a detailed introduction! , ++ X, -- X, and (t) x operators, and other operators.
1. ++ Operator
The ++ operator is also called an incremental operator. It can be placed on either the left side of the operand or the right side of the operand. If the ++ operator is placed on the left of the operand, it is called the prefix incremental operator. Otherwise, it is called the suffix incremental operator. Syntax:
++ Expression or expression ++
Expression indicates the operand. The "++ expression" expression first calculates the value of expression, then increases the value by 1, and serves as the result of this expression. The expression ++ calculates the value of expression and serves as the result of the expression. Then, it increases the value of expression by 1. Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class testincrement
05 {
06 public static void main (string [] ARGs)
07 {
08 int A = 2008; // define a variable
09 int B = ++ A; // increment the prefix and define a B Variable to save the result
10 int c = A ++; // increment the suffix and define a C variable to save the result.
11 console. writeline ("A value:" + a); // output result
12 console. writeline ("B value:" + B); // output result
13 console. writeline ("C value:" + C); // output result
14}
15}
16}
In the above Code, after the expression "++ a" is executed in row 9th, the value of variable A is 2009 and serves as the value of this expression. Therefore, the value of variable B is 2009. The expression "c = A ++" in row 10th first obtains the value of variable A and assigns the value to the variable C. Therefore, the value of variable C is 2009. Then, increase the value of variable A by 1. Therefore, the value of variable A is 2010.
Final output result:
A: 2010
B: 2009
C: 2009
2. -- operator
-- An operator, also known as the subtraction Operator, can be placed on the left side of the operand or on the right side of the operand. If the -- operator is placed on the left of the operand, it is called the prefix subtraction operator. Otherwise, it is called the suffix subtraction operator. Syntax:
-- Expression or expression --
Expression indicates the operand. The "-- Expression" expression first calculates the value of expression, then minus 1 and serves as the result of this expression. "Expression --" first calculates the value of expression and serves as the result of the expression. Then, the value of expression is reduced by 1. Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class testdeduction
05 {
06 public static void main (string [] ARGs)
07 {
08 int A = 2008; // define a variable
09 int B = -- A; // perform the prefix subtraction operation and define a B Variable to save the result.
10 int c = A --; // perform suffix subtraction and define a C variable to save the result
11 console. writeline ("A value:" + a); // output result
12 console. writeline ("B value:" + B); // output result
13 console. writeline ("C value:" + C); // output result
14}
15}
16}
In the above Code, after the expression "-- a" is executed in row 9th, the value of variable A is 2007 and serves as the value of this expression. Therefore, the value of variable B is 2007. The expression "c = A --" in the fifth line first obtains the value of variable A and assigns the value to variable C. Therefore, the value of variable C is 10th. Then, reduce the value of variable A by 1. Therefore, the value of variable A is 2006.
Final output result:
A: 2006
B: 2007
C: 2007
3 ,! Operator
! Operators are also called logical negative operators. If the value of the operand is true, the result is false. If the operand is false, the result is true. Syntax:
! Expression
Expression indicates the operand. Only boolean data can be used. Other data types cannot be used! Operator. Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class testnot
05 {
06 public static void main (string [] ARGs)
07 {
08 bool a = true; // define a variable
09 bool B =! A; // perform a logical negative operation and define a variable B to save the result.
10 console. writeline ("A value:" + a); // output result
11 console. writeline ("B value:" + B); // output result
12}
13}
14}
In the above Code, line 3 "! After the expression is executed, the value of variable A is false and serves as the expression value. Therefore, the value of variable B is equal to false.
Final output result:
The value of A is true.
The value of B is false.
4. (t) x Operator
(T) The X operator explicitly converts an expression to a specified type. The syntax is as follows:
(Type) Expression
Type indicates the type, and expression indicates the expression to be converted. Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class testconverttypes
05 {
06 public static void main (string [] ARGs)
07 {
08 object o = 2009; // define an O variable
09 int I = (INT) O; // perform explicit conversion
10 console. writeline ("I value:" + I );
11}
12}
13}
In the above Code, 9th rows (INT) O are converted to the display type, and the value of the O variable is converted to the int type. NOTE: For the (t) x expression, there is no explicit conversion from the X type to the T type, and a compilation error will occur.
Final output result:
The I value is 2009.
5. Arithmetic Operators
*,/, %, +, And-operators are called arithmetic operators. They represent multiplication, division, remainder, addition, and subtraction respectively. Syntax:
Left expression operator right expression
Left expression and right expression represent the Left and Right operations, respectively. Operator represents operators, which can be *,/, %, +, and -. Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class testnumber
05 {
06 public static void main (string [] ARGs)
07 {
08 int A = 1; // define a variable
09 int B = 1; // define a variable
10 int c = a + B; // perform arithmetic operations
11 console. writeline ("C value:" + C); // output result
12}
13}
14}
In the above Code, the value of variable A is 1, and the value of variable B is 1. After the + operation, the value of variable C is equal to 2. If the left and right operands belong to the same type, the result type is the same as that of the operand. If the checked operator is used when the operation result is out of the range indicated by this type, the system. overflowexception exception is thrown. If the unchecked operator is used, no exception is thrown, but any valid high-order bits out of the result type are discarded.
Final output result:
The value of C is 2.
For the + operator, if one or both of its operands are of the string type, the + operator performs the String concatenation operation instead of a simple arithmetic operation. Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class teststring
05 {
06 public static void main (string [] ARGs)
07 {
08 string stra = "2008"; // defines a stra variable
09 string strb = ". 08.08"; // defines a stra variable
10 string STRC = stra + strb; // concatenate string operations
11 console. writeline (STRC); // output result
12}
13}
14}
In the above Code, lines 8th and 9th define a string variable stra and strb respectively. Then we use the + operator to perform operations and save the operation results in STRC. The STRC variable value is a string of 2008.08.08.
6. logical operators
&, ^, And | operators are called logical operators. & Operator: calculates the bitwise logic and of two operands. | Operator calculates the bitwise logic or of two operands. ^ operator calculates the bitwise logic XOR of two operands. Syntax:
Left expression operator right expression
Left expression and right expression represent the Left and Right Operations respectively, and operator represents the operators, which can be &, ^, and |. Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class testlogic
05 {
06 public static void main (string [] ARGs)
07 {
08 bool a = true; // define a variable
09 bool B = false; // defines a variable.
10 bool c = A & B; // logic and Operation
11 console. writeline ("the value of the C variable is" + C); // output the result
12}
13}
14}
In the above Code, we performed logical operations on line 1. The specific rules are as follows:
If both A and B are true, the result of A & B is true. Otherwise, the result is false.
If A or B is true, the result of a | B is true. Otherwise, the result is false.
If A is true, B is false, or a is false, and B is true, the result of a ^ B is true. Otherwise, the result is false. That is, if the values of A and B are different, the result of a ^ B is true.
Final output result:
The value of the C variable is false.
7. Conditional Operators
? : An operator is called a conditional operator. It is the only ternary operator in C. Syntax:
Expression? Trueresult: falseresult;
This expression first calculates the value of the conditional expression. If the value of the conditional expression is true, the value of trueresult becomes the calculation result. Otherwise, falseresult is calculated and the result is calculated. Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class testcondition
05 {
06 public static void main (string [] ARGs)
07 {
08 int A = 10; // define a variable
09 int B = 2008; // define a B Variable
10 int c = a + B> 3000? A: B; // perform conditional operations.
11 console. writeline ("the value of the C variable is" + C); // output the result
12}
13}
14}
In the above Code, if the value of expression A + B is greater than 10th, the value of C is equal to the value of a; otherwise, the value of C is equal to the value of B.
Final output result:
The value of the C variable is 2008
8. Conditional logical operators
& | An operator is a conditional logical operator. & Operators are logical and operators, which calculate the logic of the left and right operands and. | The operator is a logical or operator that computes the logic of the left and right operands or. Syntax:
Left expression operator right expression
Left expression and right expression indicate the Left and Right Operations respectively. Operator indicates the operator, which can be & and |.
Example: Use the & operator to calculate the sum of the operands A and B (both bool type), and save the result as a C variable.
Bool a = true;
Bool B = false;
Bool c = A & B;
Analysis: the value of the C variable is false.
Note: For expression A & B, the value of B is calculated only when the value of A is true. For expression a | B, the value of B is calculated only when the value of A is false.
9. Shift Operators
<And> operators are called shift operators. <Operator indicates left shift,> operator indicates right shift. Syntax:
Expression operator count;
Expression indicates the expression to be moved, count indicates the number of digits to move, and operator indicates the operator, which can be <and>.
Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class testmove
05 {
06 public static void main (string [] ARGs)
07 {
08 int A = 2008; // define a variable
09 int C1 = A <2; // shift left
10 int C2 = A> 2; // shift to the right
11 console. writeline ("C1 variable value:" + C1); // output result
12 console. writeline ("C2 variable value:" + C2); // output result
13}
14}
15}
In the above Code, the expression a <count shifts a to the left to count a bit. The calculation method is as follows: discard the high sequence bit after the center shift in a (if these high sequence bits are retained, the result will be out of the range of the result type), and shift the remaining bits to the left, and set the null low-order bit to zero. A> count expression shifts a to the Right to count a bit. The calculation method is as follows:
When a is of the Int or long type, discard the low sequence Bit Of A and shift the remaining bit to the right. If X is not negative, the high-order vacancy position is set to zero. If a is negative, set it to 1.
When a is of the uint or ulong type, discard the low-order bit of A, shift the remaining bit to the right, and set the high-order vacant position to zero.
Final output result:
C1 variable value is 8032
The C2 variable value is 502.
10. Relational and type test operators
= ,! The =, <,>, <=,> =, is, and as operators are called relational and type test operators. Syntax:
Left expression operator right expression
Left expression and right expression represent the Left and Right operations, respectively. Operator represents the operator, which can be = ,! =, <,>, <=,> =, Is, and. Where, = ,! =, <,>, <=, And> = Are comparison operators. Their calculation methods are as follows:
X = Y. If X is equal to Y, true is used; otherwise, false is used.
X! = Y. If X is not equal to Y, true is used; otherwise, false is used.
X <Y. If X is less than Y, true is used; otherwise, false is used.
X> Y. If X is greater than Y, true is used; otherwise, false is used.
X <= Y. If X is less than or equal to Y, true is used. Otherwise, false is used.
X> = Y. If X is greater than or equal to Y, true is used. Otherwise, false is used.
Note: = ,! The results of the =, <,>, <=, and> = operators are of the bool type.
Here is an example:
01 using system;
02 namespace Microsoft. Example
03 {
04 public class testcompare
05 {
06 public static void main (string [] ARGs)
07 {
08 int A = 10; // define a variable
09 int B = 2008; // define a B Variable
10 bool c = (a = B); // compare and assign the result to the C variable
11 console. writeline ("the value of the C variable is" + C); // output the result
12}
13}
14}
In the above Code, the = Operator is used in row 10th for comparison. If a is equal to B, the result is true. If a is not equal to B, the result is false.
Final output result:
The value of the C variable is false.
11. Is Operator
The is operator is used to dynamically check whether the runtime type of the object is compatible with the given type, and the result is also of the bool type. For an "E is t" expression (where E is an expression and T is a type), if e can be implicitly converted to T, the result of this expression is true, otherwise, the value is false.
Here is an example:
1 using system;
2 namespace Microsoft. Example
3 {
4 public class TESTIS
5 {
6 static void main ()
7 {
8 int A = 10; // defines a variable.
9 bool c = A is object; // perform the is Operation
10 console. writeline ("the value of the C variable is" + C );
11}
12}
13}
In the above Code, we use the "is" operator to determine whether a is of the object type. We know that all types are inherited from the object type, and the result of all is operations is true.
Final output result:
The value of the C variable is true.
12. As Operator
The as operator is used to explicitly convert a value to a given reference type. If the conversion fails, null is returned.
Here is an example:
1 using system;
2 namespace Microsoft. Example
3 {
4 public class TESTIS
5 {
6 static void main ()
7 {
8 int A = 10; // defines a variable.
9 Object C = A as object; // perform the as Operation
10 console. writeline ("the value of the C variable is" + C. GetType ());
11}
12}
13}
In the above Code, we perform the as operator operation on line 1 and convert the integer a to the object type. Here we need to note that the conversion here is a type reference, and the actual content has not changed. We usually call this behavior a packing operation, which we will discuss later. The as operator never raises an exception. For an "E as t" expression, e must be an expression, T must be a reference type, and the calculation result can always belong to the T type.
Final output result:
The value of the C variable is system. int32.
13. assignment operators
=, * =,/=, % =, + =,-=, <=, >>=, & =, ^ =, And | = operators are called value assignment operators, they can assign new values to variables, attributes, events, or indexer elements. Syntax:
Left expression operator right expression
Left expression and right expression represent the Left and Right Operations respectively, and operator represents operators, such as =, * =, And/=.
Example: Use the * = Operator to calculate the product of two operands (a and B, their values are 10 and 2008), and assign the value to the left operand B.
Int A = 10, B = 2008;
Int B * =;
Analysis: the value of variable B is 20080.
Note: The left operand of the value assignment operator must be an expression of the variable, attribute access, indexer access, or event access type.
= An operator is called a simple value assignment operator. It assigns the value of the right operand to the variable, attribute, or indexer element given by the left operand. The OP = Operator is called the composite value assignment operator. The "X op = y" expression is equivalent to the "x = x op y" expression.
All the Simplified assignment operators in C # are listed below.
The operator's simplified operations are equal
X ++, ++ x = x + 1
X--,--x = x-1
X + = Y x = x + y
X-= Y x = x-y
X * = Y x = x * y
X/= Y x = x/y
X % = Y x = x % Y
X> = Y x = x> Y
X <= Y x = x <Y
X & = Y x = x & Y
X | = Y x = x | y
X ^ = Y x = x ^ y
Why are two examples used to describe the plus (+) Increment and minus (-) operators? Operators are placed before the expression, and operators are placed behind the expression. Their execution methods are different.

 

1.4.2 operator priority

When an expression contains multiple operators, the operator priority controls the operator's computational order. For example, the expression x + y * z is calculated based on X + (y * z) because the operator * has a higher priority than +. The operator priority is determined by the definition of the relevant expression of the operator.
These operators are outlined below and listed in a descending order of priority:
Basic x. y f (x) A [x] x ++ X -- new typeof checked unchecked
One dollar + -! ~ ++ X -- x (t) x
Multiplication */%
Addition +-
Shift <>
Link and type detection <? >? <=? >=? Is? As
Equal =! =
Logic XOR ^
Logic or |
Conditions and &&
Condition or |
Condition? :
Assignment = * =/= % = + =-= <=? >>=& = ^ = | =
When the operands appear between two operators with the same priority, the operator execution sequence is as follows:
Except the value assignment operator, all binary operators are associated with the left sequence, meaning that operations are performed from left to right. For example, X + Y + Z is calculated by (x + y) + Z.
Assignment operators and conditional operators (? :) Right-Order Association, meaning operations are performed from right to left. For example, x = y = z is calculated by X = (y = z.
The priority and order associativity can be controlled in parentheses. For example, x + y * z first times y by z and then adds the result to X, while (x + y) * z first adds X and Y, then multiply the result by Z.
In complex expressions, you should avoid using operator priority to generate correct results. Using parentheses to specify the execution sequence of operators can make the code more clean and avoid potential conflicts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.