C # Basic Knowledge series 4 (operator summary)

Source: Internet
Author: User

This section describes various operators in C. It mainly includes the is operator, as operator, checked and unchecked operator, sizeof operator, and null join operator (??) , &, Shift operator, increment and decrement operator, conditional operator (ternary operator), namespace alias qualifier.

1. is Operator

The is operator checks whether the object is compatible with a specific type. For example, in the following example, check whether the variable is compatible with the object type:

 i=(i  

 

2. as Operator

The as operator is used to perform explicit type conversion of the reference type. If the type to be converted is compatible with the specified type, the conversion is successful. If the type is incompatible, The as operator returns null. Example:

          Main( obj1 =  obj2 =  str1 = obj1   str2 = obj2  

Note: The as operator allows secure type conversion in one step. You do not need to use the is operator to test the type before performing the conversion.

 

3. checked and unchecked Operators

          Main( b = +++ c = ++; 

The byte data type can only contain 0 ~ 255, so the incremental value of B will overflow. How does CLR handle overflow? C # provides the checked and unchecked operators. If a piece of code segment is marked as checked, CLR will execute the overflow check. If an exception occurs, an exception will be thrown.

To disable the overflow check, mark the code as unchecked.

Note: unchecked is the default value. The unchecked keyword must be explicitly used only when several unchecked code lines need to be placed in a large code block explicitly marked as checked.

 

4. sizeof Operator

Sizeof can be used to determine the length required for the stack value type (in bytes ):

 

 

5. null join operator (??)

The Null join operator provides a shortcut for processing the possibility of representing a Null value when an empty or referenced type is available. This operator is placed between two operands. The first operand must be of a null or reference type, and the second operand must be of a different type from the first operand, alternatively, it can be implicitly converted to the type of the first operand. The calculation of the null join operator is as follows: if the first operand is not null, the entire expression is equal to the value of the first operand. However, if the first operand is null, the entire expression is equal to the value of the second operand. For example:

?a==a??; a==a??;

6 &&

  &:Binary operators (&) pre-define Binary & operators for integer and bool types. For an integer, & calculates the bitwise "and" of the operands ". For bool operands, & calculate the logic of the operands "and"; that is, the result is true only when both operands are true.

&&:The condition "and" Operator (&) executes the logical "and" Operation of the Boolean operand, but only calculates the second operand if necessary. It is similar to the binary operator (&). The difference is that if x is false, y is not calculated (because no matter what value y is, the result of the operation is false ). This is called a "Short Circuit" calculation.

A little bitBitwise "and":

Calculation rule: 0 & 0 = 0; 0 & 1 = 0; 1 & 0 = 0; 1 & 1 = 1;

That is, if the two digits are "1" at the same time, the result is "1". Otherwise, the value is 0.

For example, 3 & 5 means 0000 0011 & 0000 0101 = 0000 0001. Therefore, 3 & 5 is worth 1.

 

5 & 7 is 0000 0101 & 0000 0111 = 0000 0101. Therefore, 5 & 7 is worth 5.

 

7. Shift Operators

The <(left shift) and> (right shift) operators are used to execute the shift operation.

Shift left (<)

Move the first operand to the left and the number of digits specified by the second operand.
The Left shift is equivalent to multiplication. The left shift is equivalent to multiplication 2; the left shift is equivalent to multiplication 4; The Left shift is equivalent to multiplication 8.

X <1 = x * 2
X <2 = x * 4
X <3 = x * 8
X <4 = x * 16

Similarly, the right shift is the opposite:

Shift right (>)
Move the first operand to the right to the number of digits specified by the second operand, and fill in 0 for the null position.

The right shift is equivalent to the entire division. The right shift is equivalent to dividing by 2; the right shift is equivalent to dividing by 4; The right shift is equivalent to dividing by 8.

X> 1 = x/2
X> 2 = x/4
X> 3 = x/8
X> 4 = x/16

When declaring to overload the C # shift operator (this also has an overload character "+" http://www.cnblogs.com/aehyok/p/3499822.html in section 1), the type of the first operand must always contain the class or structure declared by the operator, and the type of the second operand must always be int, for example:

      Main(=  ShiftClass(, = shift1 << = shift1 >>     ShiftClass( valA, .valA =.valB =  ShiftClass  <<(ShiftClass shift,  a = shift.valA << b = shift.valB <<   ShiftClass  >>(ShiftClass shift,  a = shift.valA >> b = shift.valB >> 

 

8. Increment and decrement operators and conditional operators (ternary operators)

This in the first section also mentioned http://www.cnblogs.com/aehyok/p/3499822.html

9. namespace alias qualifier

Suppose you have implemented the following code and want to print it:

Console.WriteLine();

Next, we will use the namespace for access:

You will find that these two methods cannot be implemented. You can call it as follows:

         ::System.Console.WriteLine(

Global is C #2.0.
In theory, if the code is well written, it is not required at all.

In the above Code, a class named System is written. Then, when you write System in the code
The compiler does not know whether you are writing the System class or the System namespace.
The namespace is already the root namespace and cannot be specified by a full name restriction. In the previous C # version, this is an unsolved problem. Now, you can use global: System to represent the System root namespace, and use your own MyNamespace. System
To represent your own class.

Next, let's look at a small example:

 colAlias =             colAlias::Hashtable test =             test.Add(, , ,  ( name                 ::System.Console.WriteLine(name +   +

 

Related Article

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.