C # overview of various operators and functions of operators,

Source: Internet
Author: User
Tags modulus

C # overview of various operators and functions of operators,
I. []

Square brackets ([]) are used for arrays, indexers, and attributes, and can also be used for pointers.

1. the array type is a type followed:

Int [] fib = new int [100]; // create an array with 100 Elements

To access an element of an array, enclose the index of the required element in square brackets:

Fib [0] = fib [1] = 1;

For (int I = 2; I <100; ++ I) fib [I] = fib [I-1] + fib [I-2]; // If the array index is out of range, an exception is thrown.

2. array index operators cannot be reloaded. However, you can define indexes and attributes that use one or more parameters. The indexer parameters are enclosed in square brackets (just like an array index), but the indexer parameters can be declared to be of any type (different from the array index, the array index must be an integer ).

For example,. NET Framework defines the Hashtable type, which associates the key with any type of value.

Collections. Hashtable h = new Collections. Hashtable ();

H ["a"] = 123; // note: using a string as the index

3. square brackets are also used to specify attributes (C # programming guide ):

[Attribute (AllowMultiple = true)]

Public class Attr

{

}

4. You can use square brackets to specify the pointer index:

Unsafe fixed (int * p = fib) // p points to fib from earlier example

{

P [0] = p [1] = 1;

For (int I = 2; I <100; + I) p [I] = p [I-1] + p [I-2];

}

II ,()

In addition to specifying the operation sequence in the expression, parentheses are also used to specify forced conversion or type conversion:

Double x = 1234.7;

Int;

A = (int) x; // cast double to int

Iii ,.

The vertex operator (.) is used for member access. The vertex operator specifies the type or namespace member. For example, the dot operator is used to access a specific method in the. NET Framework Class Library:

System. Console. WriteLine ("hello ");

Iv ,::

Namespace alias qualifier operator (: :), used to find the identifier. It is usually placed between two identifiers, for example:

Global: System. Console. WriteLine ("Hello World ");

Note:

The namespace alias can be global. This will call the search in the global namespace, rather than in the alias namespace.

V. +

+ The operator can be either a unary or a binary operator.

Note:

The mona1 + operator is predefined for All numeric types. The result of a one-dollar + operation on the value type is the value of the operand.

The binary + operator is predefined for numeric and string types. For the value type, + calculates the sum of the two operands. When one of the operands is a string or both are strings, + Concatenates the string representation of the operands.

The delegate type also provides binary + operators, which execute delegation concatenation.

Instance:

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine (+ 5); // unary plus

Console. WriteLine (5 + 5); // addition

Console. WriteLine (5 +. 5); // addition

Console. WriteLine ("5" + "5"); // string concatenation

Console. WriteLine (5.0 + "5"); // string concatenation

// Note automatic conversion from double to string

}

}

Vi ,-

-The operator can be either a unary or a binary operator.

The Unary-operator is predefined for All numeric types. The result of a value-type unary operation is the inverse Number of the operand.

The binary-operator is pre-defined for all numeric and enumeration types. Its function is to subtract the second operand from the first operand.

The delegate type also provides a binary-operator that executes the delegate removal.

Example:

Using System;

Class MainClass

{

Static void Main ()

{

Int a = 5;

Console. WriteLine (-);

Console. WriteLine (a-1 );

Console. WriteLine (a-. 5 );

}

}

VII ,*

Multiplication operator (*) is used to calculate the product of the operand. It is also used to cancel the reference operator and allow reading and writing pointers.

Note:

All numeric types have predefined multiplication operators.

* The operator is also used to declare the pointer type and cancel the reference pointer. This operator can only be used in unsafe contexts, expressed by the use of the unsafe keyword, and requires/unsafe compiler options. The unreferenced operator is also called an indirect addressing operator.

Example:

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine (5*2 );

Console. WriteLine (-. 5 *. 2 );

Console. WriteLine (-. 5 m *. 2 m); // decimal type

}

}

8 ,/

The Division operator (/) removes the first operand from the second operand. All numeric types have predefined division operators.

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine (-1, 5/2 );

Console. WriteLine (-5.0/2 );

}

}

IX. %

The modulus operator (%) calculates the remainder of the second operand except the first operand. All numeric types have predefined modulus operators.

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine (5% 2); // int

Console. WriteLine (-5% 2); // int

Console. WriteLine (5.0% 2.2); // double

Console. WriteLine (5.0 m % 2.2 m); // decimal

Console. WriteLine (-5.2% 2.0); // double

}

}

10 ,&

& Operators can be both unary and binary operators.

Note:

The address of the operand returned by the unsafe operator ).

The binary & operator is predefined for integer and bool types. For an integer, & the logic of the calculation operand is bitwise "and ". For bool operands, & calculate the logic of the operands "and"; that is, the result is true only when both operands are true.

& The operator calculates two operators, regardless of the value of the first operand. For example:

Int I = 0;

If (false & ++ I = 1)

{

}

XI. |

Binary | the operator is pre-defined for integer and bool types. For an integer, | returns the bitwise "or" result of the calculated operand. For bool operands, | calculates the logical "or" result of the operands. That is, if and only if both operands are false, the result is false.

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine (true | false); // logical or

Console. WriteLine (false | false); // logical or

Console. WriteLine ("0x {0: x}", 0xf8 | 0x3f); // bitwise or

}

}

12, ^

Binary ^ operators are predefined for integer and bool types. For an integer, ^ returns the bitwise XOR of the calculated operand ". For the bool operand, the logic of the ^ calculated operand is "exclusive or". That is to say, the result is true only when only one operand is true.

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine (true ^ false); // logical exclusive-or

Console. WriteLine (false ^ false); // logical exclusive-or

// Bitwise exclusive-or:

Console. WriteLine ("0x {0: x}", 0xf8 ^ 0x3f );

}

}

Thirteen ,!

Logical non-operator (!) Is a unary operator that returns the inverse of an operand. This operator is defined for bool and returns true only when the operand is false.

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine (! True );

Console. WriteLine (! False );

}

}

14th ,~

~ Operators perform bitwise complement operations on the operands. The result is equivalent to reversing each bit. The bitwise complement operator is pre-defined for the int, uint, long, And ulong types.

Using System;

Class MainClass

{

Static void Main ()

{

Int [] values = {0, 0x111, 0 xfffff, 0x8888, 0x22000022 };

Foreach (int v in values)

{

Console. WriteLine ("~ 0x {0: x8} = 0x {1: x8} ", v ,~ V );

}

}

}

15th, =

The value assignment operator (=) stores the value of the right operand in the storage location, attribute, or indexer indicated by the left operand and returns the value as a result. The operand type must be the same (or the right operand must be implicitly converted to the left operand type ).

Using System;

Class MainClass

{

Static void Main ()

{

Double x;

Int I;

I = 5; // int to int assignment

X = I; // implicit conversion from int to double

I = (int) x; // needs cast

Console. WriteLine ("I is {0}, x is {1}", I, x );

Object obj = I;

Console. WriteLine ("boxed value = {0}, type is {1 }",

Obj, obj. GetType ());

I = (int) obj;

Console. WriteLine ("unboxed: {0}", I );

}

}

16th, <

All values and enumeration types define the "less than" relational operator (<). If the first operand is smaller than the second operand, the operator returns true; otherwise, false.

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine (1 <1.1 );

Console. WriteLine (1.1 <1.1 );

}

}

17.>

All numeric and enumeration types define the "greater than" relational operator>. If the first operand is greater than the second operand, true is returned; otherwise, false is returned.

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine (1.1> 1 );

Console. WriteLine (1.1> 1.1 );

}

}

18th ,? :

Conditional operators (? :) Return one of the two values based on the value of the Boolean expression. The format of conditional operators is as follows:

Condition? First_expression: second_expression;

Note:

If the condition is true, the first expression is calculated and Its Calculation Result prevails. If the condition is false, the second expression is calculated and Its Calculation Result prevails. Calculate only one of the two expressions.

Conditional operators can be used to describe the computation that may require the if-else structure in a more concise and elegant manner. For example, to avoid division by zero in the calculation of sin function, you can write

If (x! = 0.0) s = Math. Sin (x)/x; else s = 1.0;

Or use conditional operators,

S = x! = 0.0? Math. Sin (x)/x: 1.0;

19th, ++

The incremental operator (++) adds 1 to the operand. Incremental operators can appear before or after the operands:

Note:

The first form is the prefix incremental operation. The result of this operation is the value after the operand is added with 1.

The second form is the suffix incremental operation. The result of this operation is the value before the operand is increased.

Numeric and enumeration types have predefined incremental operators. User-Defined types can be overloaded with the ++ operator. Integer operations are usually allowed during enumeration.

20 ,--

The subtraction operator (--) reduces the operand by 1. The subtraction operator can appear before or after the operand: -- variable and variable --. The first form is the prefix reduction operation. The result of this operation is the value after the operand is reduced. The second form is the suffix reduction operation. The result of this operation is that the number of operations reduces the value before.

Note:

Numeric and enumeration types have predefined incremental operators.

The user-defined type can be overloaded-operators (see operators ). Integer operations are usually allowed during enumeration.

21 ,&&

The condition "and" Operator (&) executes the logical "and" operation of its bool operations, but calculates the second operand only when necessary.

Note:

Operation

X & y

Corresponding to the operation

X & y

The difference is that if x is false, y is not calculated (because the result of the "and" operation is false regardless of the value of y ). This is called a "Short Circuit" calculation.

The condition "and" operator cannot be overloaded. However, the normal logical operators and the true and false operators are overloaded as conditional logical operators under some restrictions.

22. |

The condition "or" Operator (|) executes the logical "or" Operation of the bool operand, but calculates the second operand only when necessary.

Note:

Operation

X | y

Corresponding to the operation

X | y

The difference is that if x is true, y is not calculated (because the result of the "or" operation is true regardless of the value of y ). This is called a "Short Circuit" calculation.

The condition "or" operator cannot be overloaded, but the rule logical operator and the overload of the true and false operators are also considered as the overload of the conditional logical operator under some restrictions.

23. <

The left-shift operator (<) moves the first operand to the number of digits specified by the second operand. The type of the second operand must be int.

Note:

If the first operand is int or uint (32 digits), the number of shifts is given by the lower five digits of the second operand.

If the first operand is long or ulong (64-digit), the number of shifts is given by the Lower 6 bits of the second operand.

The high-order bit of the first operand is discarded, and the low-order vacancy is filled with 0. The shift operation never causes overflow.

User-Defined types can be overloaded <operator (see operator); the type of the first operand must be the user-defined type, and the type of the second operand must be int. When a binary operator is overloaded, the corresponding value assignment operator is also implicitly reloaded (if any ).

24.>

The right shift operator (>) moves the first operand to the right to the number of digits specified by the second operand.

Note:

If the first operand is int or uint (32 digits), the number of shifts is given by the lower five digits of the second operand (the second operand & 0x1f ).

If the first operand is long or ulong (64-digit), the number of shifts is given by the lower six bits of the second operand (the second operand & 0x3f ).

If the first operand is int or long, the right shift is the arithmetic shift (the High Order vacancy is set to the symbol bit ). If the first operand is of the uint or ulong type, the right shift is a logical shift (with a high fill of 0 ).

User-Defined types can be reloaded> operators; the type of the first operand must be user-defined, and the type of the second operand must be int. For more information, see operator. When a binary operator is overloaded, the corresponding value assignment operator is also implicitly reloaded (if any ).

25, =

For pre-defined value types, if the value of the operand is equal, the equal operator (=) returns true; otherwise, the return value is false. For reference types other than string, if the two operands reference the same object, = returns true. For the string type, = compares the string value.

Note:

The user-defined value type can be overloaded = operator (see operator ). The user-defined reference type can also be overloaded with the = Operator, although by default, no matter whether it is a pre-defined reference type or a user-defined reference type, = is the same as described above. If the reload is =, the reload is required! =. Integer operations are usually allowed during enumeration.

26 ,! =

If the operands are equal, the unequal operators (! =) Returns false. Otherwise, returns true. The unequal operators are predefined for all types (including strings and objects. User-Defined types can be reloaded! = Operator.

Note:

For predefined value types, unequal operators (! =) Returns true. Otherwise, returns false. For reference types other than string, if two operands reference different objects, then! = Returns true. For the string type ,! = Compare the string value.

User-Defined value types can be reloaded! = Operator (see operator ). The user-defined reference type can also be reloaded! = Operator, although by default, no matter whether it is a pre-defined reference type or a user-defined reference type ,! =. If it is reloaded! =, You must also load =. Integer operations are usually allowed during enumeration.

27th, <=

All numeric and enumeration types define the "less than or equal to" relational operator (<=). If the first operand is less than or equal to the second operand, the operator returns true, otherwise, false is returned.

28th,> =

All numeric and enumeration types define "greater than or equal to" Relational operators> =. If the first operand is greater than or equal to the second operand, true is returned. Otherwise, false is returned.

19th, + =

Addition assignment operator.

Note:

The expression using the + = value assignment operator, for example

X + = y

Equivalent

X = x + y

The difference is that x is calculated only once. + The meaning of the operator depends on the type of x and y (for example, for a numeric operand, it means addition; for a string operand, it means concatenation ).

You cannot directly overload the + = operator, but the User-Defined type can be reloaded with the + operator (see operator ).

Thirty,-=

Subtraction value assignment operator.

Note:

The expression using the-= value assignment operator, as shown in figure

X-= y

Equivalent

X = x-y

The difference is that x is calculated only once. -The meaning of the operator depends on the x and y types (for example, for numeric operands, the meaning is subtraction; for delegate operands, the meaning is removed ).

The-= operator cannot be directly overloaded, but the User-Defined type can be reloaded-operator (see operator ).

, * =

Binary multiplication assignment operator.

Note:

The expression that uses the * = value assignment operator, as shown in figure

X * = y

Equivalent

X = x * y

The difference is that x is calculated only once. * Operators are predefined for the numeric type to perform multiplication.

* = Operators cannot be directly overloaded, but user-defined types can be reloaded * operators (see operator ).

,/=

Division assignment operator.

Note:

The expression that uses the/= value assignment operator, as shown in figure

X/= y

Equivalent

X = x/y

The difference is that x is calculated only once. Pre-defines the/operator for the numeric type to perform the division operation.

The/= operator cannot be directly overloaded, but user-defined types can be reloaded/operators (see operator ). For all compound assignment operators, the implicit overload binary operator will overload the equivalent compound assignment.

33, % =

Module assignment operator.

Note:

The expression using the % = value assignment operator, as shown in figure

X % = y

Equivalent

X = x % y

The difference is that x is calculated only once. The % operator is predefined for the value type to calculate the remainder after division.

You cannot directly overload the % = Operator, but the User-Defined type can overload the % operator (see operator (C # reference )).

34, & =

"And" value assignment operator.

Note:

The expression using the & = value assignment operator, as shown in figure

X & = y

Equivalent

X = x & y

The difference is that x is calculated only once. & Operators perform bitwise logical "and" operations on Integer operands, and perform logical "and" operations on bool operands.

The & = operator cannot be directly overloaded, but Binary & operators can be reloaded for user-defined types (see operator ).

35, | =

"Or" value assignment operator.

Note:

The expression that uses the | = value assignment operator, for example

X | = y

Equivalent

X = x | y

The difference is that x is calculated only once. | The operator performs the bitwise logical "or" operation on Integer operands and the logical "or" operation on Boolean operands.

Cannot be directly overloaded | = operator, but user-defined types can be reloaded | operator (see operator ).

36, ^ =

"XOR" value assignment operator.

Note:

Expressions in the following forms

X ^ = y

The calculation is based on the following rules:

X = x ^ y

The difference is that x is calculated only once. ^ Operators perform bitwise "XOR" operations on Integer operands, and perform logical "XOR" operations on bool operands.

You cannot directly reload the ^ = Operator, but the User-Defined type can be reloaded! Operator (see operator ).

37. <=

Left shift assignment operator.

Note:

Expressions in the following forms

X <= y

The calculation is based on the following rules:

X = x <y

The difference is that x is calculated only once. <Operator moves x to the specified number of digits of y.

You cannot directly reload the <= operator, but the User-Defined type can be reloaded <operator (see operator ).

38.> =

Shifts the value assignment operator to the right.

Note:

Expressions in the following forms

X> = y

The calculation is based on the following rules:

X = x> y

The difference is that x is calculated only once.> The operator shifts the right of x based on the amount specified by y.

You cannot directly reload the operators> =, but the user-defined types can be reloaded> operators (see operator ).

39,->

-> The operator combines the unreferenced pointer with the member access.

Note:

Expressions in the following format

X-> y

(X is a pointer of the T * type, and y is a member of the T type .)

(* X). y

-> Operators can only be used in unmanaged code.

You cannot reload the-> operator.

Forty ,??

If ?? The left operand is not null. This operator returns the left operand. Otherwise, the right operand is returned.

Note:

The value can be null or undefined .?? The operator defines the default value returned when an empty type is assigned to a non-empty type. If you do not use the question mark (?) when assigning an empty type to a non-empty type ?? It will generate an error during compilation. If forced conversion is used, and the currently undefinable null type is not defined, an InvalidOperationException occurs.

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.