C # Operator Summary

Source: Internet
Author: User
C # mainly supports the following operators: Code
Arithmetic Operator +-*/%
Logical operators ~ & |!
String concatenation operator +
Increment and Decrement Operators ++ --
Shift Operator < < > >
Comparison operator =! = <   >   < = > =
Assignment operator = + =-= * =/= % = | = ^ = < <= > > =
Member access operators (used for objects and structures ).
Index operators (for arrays and indexers) []
Data type conversion operator ()
Conditional operator (ternary operator )? :
Delegate join and delete operators +-
Object creation operator new
Type Information operator sizeof (used only for insecure Code ) Is as typeof
Overflow exception control operator checked unchecked
Indirect addressing operator *-> & (used only for Insecure code) []
Namespace alias qualifier ::
Null join operator ??

Operators that are not commonly used but are very important:
1. Is and as operators
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: IntI=0;
If(IIs Object)
{
Console. writeline ("I is an object.");
}

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: Object Obj1 = " A string " ;
Object Obj2 = 5 ;
String Str1 = Obj1 As   String ; // Str1 = "a string ";
String Str2 = Obj2 As   String ; // Str2 = NULL;

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.
2. Checked and unchecked Operators
The following code is available: ByteB=255;
B++;
Console. writeline (B. tostring ());

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.

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> byte B = 255 ;
checked
{< br> B + ; /// throw overflowexception
}< br> console. writeline (B. tostring ();

to disable the overflow check, mark the code as unchecked:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> byte B = 255 ;
unchecked
{< br> B + ;< BR >}< br> console. writeline (B. tostring ();

In the above Code, no exception is thrown, but data is lost. Because the byte data type cannot contain 256 bits, the overflow bits are lost, and the value of variable B is 0.
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.
3. sizeof Operator
Anyone who has learned C knows that sizeof can be used to determine the length required for the stack's median type (in bytes ): Unsafe
{
Console. writeline (Sizeof(Int));//The result is 4, because int has four bytes, 32 bits
}

4. Null Types and operators
If Program You must consider the impact of the null value when using it with various operators. Generally, when the null type is used together with a mona1 or binary operator, if one or both of the operands are null, the result is null. Int ? A =   Null ;
Int ? B = A + 4 ; // B = NULL;
Int ? C = A * 2 ; // C = NULL;

Note: When comparing Null Types, only one operand is null, and the comparison result is false. That is, it cannot be considered that the opposite of a condition is true because it is false. Int ? A = Null ;
Int ? B = 10 ;
If ( > = B) // Null Value A obviously cannot be compared with B
Console. writeline ( " A> = B " );
Else
Console. writeline ( " A <B " );

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 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: Int ? A = Null ;
Int B;
B = A ?? 10 ; // The value of B is 10;
A = 15 ;
B = A ?? 10 ; // The value of B is 15;

Note: If the second operand cannot be implicitly converted to the type of the first operand, a compilation error is generated.

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.