General description and function of C # operator Encyclopedia _ various operation symbols

Source: Internet
Author: User
Tags sin

The following is a detailed summary of the description and function of various operational symbols in C # first, []

square brackets ([]) are used for arrays, indexers, and properties, and also for pointers.

1, the array type is a type followed by []:
int[] fib = new int[100]; Create an array with 100 elements
To access an element of an array, enclose the index of the desired 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 overloaded, but types can define indexers and properties that take one or more parameters. The indexer parameter is enclosed in square brackets (just like an array index), but the indexer parameter can be declared as any type (unlike an array index, the array index must be an integer).
For example, the. NET Framework defines a Hashtable type that associates keys with any type of value.
Collections.hashtable h = new Collections.hashtable ();
H["a"] = 123; Note:using a string as the index

3. 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 index of the pointer:
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];
}

Second, ()
In addition to specifying the order of operations in an expression, parentheses are used to specify casts or type conversions:
Double x = 1234.7;
int A;
A = (int) x; Cast double to int

Three.
The dot operator (.) is used for member access. The point operator specifies the type or member of the namespace. For example, the point operator is used to access specific methods in the. NET Framework class Library:
System.Console.WriteLine ("Hello");

Four,::
namespace alias qualifier operator (::), used to find identifiers. It is usually placed between two identifiers, for example:
Global::system.console.writeline ("Hello World");
Note: The
namespace alias qualifier can be global. This invokes the lookup in the global namespace, not in the alias namespace. The

five, +
+ operators can be either unary operators or two-dollar operators.
Note:
The unary + operator is predefined for all numeric types. The result of a unary + operation on a numeric type is the value of the operand. The
defines a two + operator for numeric types and string types. For a numeric type, + calculates the sum of two operands. When one of the operands is a string type or two operands are of type string, + concatenates the string representations of the operands. The
delegate type also provides a two-dollar + operator that performs a delegate 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
}
}

Six,-
The-operator can be used as a unary operator or as a two-dollar operator.
Unary-operators are predefined for all numeric types. The result of a unary-operation of a numeric type is the inverse of the operand.
The binary-operator is predefined for all numeric types and enumeration types, and its function is to subtract the second operand from the first operand.
The delegate type also provides a two-dollar-operator, which performs a delegate removal.

Example:
Using System;
Class MainClass
{
static void Main ()
{
int a = 5;
Console.WriteLine (-a);
Console.WriteLine (A-1);
Console.WriteLine (A-. 5);
}
}

Seven, *
The multiplication operator (*), used to calculate the product of the operand. It is also used as a dereference operator to allow reading and writing pointers.
Note:
All numeric types have pre-defined multiplication operators.
The * operator is also used to declare pointer types and dereference pointers. The operator can only be used in an insecure context, expressed through the use of the unsafe keyword, and requires the/unsafe compiler option. The dereference operator is also called an indirection operator.

Example:
Using System;
Class MainClass
{
static void Main ()
{
Console.WriteLine (5 * 2);
Console.WriteLine (-.5 *. 2);
Console.WriteLine (-.5m *. 2m); Decimal type
}
}

Eight,/
The division operator (/) uses the second operand in addition to the first operand. All numeric types have pre-defined division operators.
Using System;
Class MainClass
{
static void Main ()
{
Console.WriteLine ( -5/2);
Console.WriteLine ( -5.0/2);
}
}

IX,%
modulo operator (%) Calculates the remainder of the second operand, with the exception of the first operand. All numeric types have pre-defined modulo 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.0m 2.2m); Decimal
        Console.WriteLine ( -5.2 2.0);//Double
    }
}

Ten, &
The & operator can be either a unary operator or a two-dollar operator.
Note:
The unary & operator returns the address of the operand (requires an unsafe context).
The binary & operator is pre-defined for the integral type and bool type. For integer,&, the Logical bitwise "and" of the operands are computed. The logical "and" of the operands is computed for the bool operand,&, that is, the result is true if and only if the two 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 operators are predefined for integer and bool types. For integral types, | Calculates the bitwise or result of the operand. for bool operands, | Computes the logical or result of the operand, that is, the result is false if and only if the two operands are 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
}
}

The

12, ^
Two ^ operators are predefined for integer and bool types. For integral types, ^ calculates the bitwise XOR of the operands. for bool operands, ^ calculates the logical "XOR" of the operand, which means that the result is true if and only if 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);
   }
}

13,!
The logical non-operator (!) is the unary operator that deserializes the operand. The operator is defined for bool and returns true if and only if the operand is false.
Using System;
Class MainClass
{
static void Main ()
{
Console.WriteLine (!true);
Console.WriteLine (!false);
}
}

14, ~
The ~ operator performs a bitwise complement operation on the operand, which is equivalent to reversing each bit. The bitwise complement operator is predefined for int, uint, long, and ulong types.
Using System;
Class MainClass
{
static void Main ()
{
Int[] values = {0, 0x111, 0xFFFFF, 0x8888, 0x22000022};
foreach (int v in values)
{
Console.WriteLine ("~0x{0:x8} = 0x{1:x8}", V, ~v);
}
}
}

XV, =
The assignment operator (=) stores the value of the right operand in the storage location, property, or indexer represented by the left operand, and returns the value as a result. The operands must be of the same type (or the right operand must be implicitly convertible to the type of the left-hand operand).
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 am {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);
}
}

16. <
All numeric and enumeration types define a "less than" relational operator (<), which returns true if the first operand is less than the second operand, otherwise false.
Using System;
Class MainClass
{
static void Main ()
{
Console.WriteLine (1 < 1.1);
Console.WriteLine (1.1 < 1.1);
}
}

17. >
All numeric types and enumeration types define a "greater than" relational operator, and if the first operand is greater than the second operand, it returns true, otherwise false is returned.
Using System;
Class MainClass
{
static void Main ()
{
Console.WriteLine (1.1 > 1);
Console.WriteLine (1.1 > 1.1);
}
}

18,?:
The conditional operator (?:) returns one of two values based on the value of a Boolean expression. The format of the conditional operator is as follows
Condition? First_expression:second_expression;

Note:
If the condition is true, the first expression is evaluated and the result of its calculation is the same, and if False, the second expression is evaluated and the result of its calculation prevail. Only one of the two expressions is evaluated.

Using conditional operators, you can express more succinctly and elegantly those calculations that might otherwise require a if-else structure. For example, to avoid being removed by 0 in the calculation of the SIN function, you can write as
if (x = 0.0) s = Math.sin (x)/x; else S = 1.0;
or use the conditional operator,
s = x! = 0.0? Math.sin (x)/x:1.0;

19, + +
The increment operator (+ +) adds 1 to the operand. The increment operator can appear before or after the operand:

Note:
The first form is the prefix increment operation. The result of this operation is the value after the operand plus 1.
The second form is postfix increment operations. The result of this operation is the value before the operand increases.
Numeric types and enumeration types have predefined increment operators. User-defined types can overload the + + operator. Integer operations are generally allowed at enumeration time.

20 、--
The decrement operator (--) minus 1 of the operand. The decrement operator can appear before or after the operand:--variable and variable--。 The first form is the prefix decrement operation. The result of this operation is that the operand is reduced to a value of "after". The second form is the suffix decrement operation. The result of this operation is that the operand is reduced to a value of "before".

Note:
Numeric types and enumeration types have predefined increment operators.
User-defined types can be overloaded with-operators (see Operators). Integer operations are generally allowed at enumeration time.

21. &&
The condition and operator (&&) performs the logical AND operation of its bool operand, but computes the second operand only if necessary.
Note:
Operation
X && y
Corresponds to the operation
X & Y
The difference is that if x is False, Y is not evaluated (because, regardless of the value of Y, the "and" action results are false). This is referred to as a "short circuit" calculation.

The condition "and" operators cannot be overloaded, but overloads of the general logical operators and operators true and false are also considered overloads of the conditional logical operators under certain constraints.

22, | |
The condition or operator (| |) executes the logical OR operation of a bool operand, but computes the second operand only if necessary.
Note:
Operation
x | | Y
Corresponds to the operation
x | Y
The difference is that if x is true, Y is not evaluated (because the result of the or operation is true regardless of the Y value). This is referred to as a "short circuit" calculation.

The conditional or operator cannot be overloaded, but the rule logical operator and the operator true and false overloads are also considered overloads of the conditional logical operators under certain constraints.

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

Note:
If the first operand is an int or uint (32-digit number), the number of shifts is given by a low 5 bits of the second operand.
If the first operand is a long or ulong (64-digit number), the shift number is given by a low 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. A shift operation never causes an overflow.
User-defined types can overload the << operator (see operator), the first operand must be of the user-defined type, and the second operand must be of type int. When you overload the two-tuple operator, the corresponding assignment operator, if any, is also implicitly overloaded.

24. >>
The right-shift operator (>>) shifts the first operand to the right by the number of bits specified by the second operand.

Note:
If the first operand is an int or uint (32-digit number), the number of shifts is given by the lower five bits of the second operand (the second operand & 0x1f).

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

If the first operand is an int or long, the right shift is an arithmetic shift (the high-order vacancy is set to the sign bit). If the first operand is a uint or ulong type, the right shift is a logical shift (high fill 0).

A user-defined type can overload the >> operator, the first operand must be of the user-defined type, and the second operand must be of type int. For more information, see operator. When you overload the two-tuple operator, the corresponding assignment operator, if any, is also implicitly overloaded.

25, = =
For predefined value types, the equality operator (= =) returns True if the value of the operand is equal, otherwise false is returned. For reference types other than string, if the two operands refer to the same object, = = Returns True. for string type, = = compares the value of a string.

Note:
User-defined value types can overload the = = operator (see operator). User-defined reference types can also overload the = = operator, although by default, = = behaves the same as described above, regardless of the predefined reference type or user-defined reference type. If you overload = =, you must also overload! =. Integer operations are generally allowed at enumeration time.

26.! =
If the operands are equal, the inequality operator (! =) returns FALSE, otherwise, returns True. The inequality operator is pre-defined for all types, including strings and objects. User-defined types can overload the! = operator.

Note:
For predefined value types, if the operands have different values, the inequality operator (! =) returns True, otherwise, false is returned. For reference types other than string,! = Returns True if the two operands reference a different object. For string type,! = compares the value of a string.

A user-defined value type can overload the! = operator (see operator). User-defined reference types can also overload the! = operator, although by default the! = behavior is the same as described above, regardless of the predefined reference type or user-defined reference type. If you overload! =, you must also overload = =. Integer operations are generally allowed at enumeration time.

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

28, >=
All numeric types and enumeration types define a "greater than equals" relational operator, >=, which returns true if the first operand is greater than or equal to the second operand, otherwise false.

29. + =
The addition assignment operator.
Note:
An expression that uses the + = assignment operator, such as
x + = y
is equivalent to
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 numeric operands, its meaning is additive; for string operands, it means concatenation).

The + = operator cannot be overloaded directly, but user-defined types can overload the + operator (see operator).

30,-=
Subtraction assignment operator.
Note:
An expression that uses the-= assignment operator, such as
X-= y
is equivalent to
x = XY
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 numeric operands, the meaning is subtraction, and the meaning for the delegate operand is remove).

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

31, *=
Binary multiplication assignment operator.
Note:
An expression that uses the *= assignment operator, such as
X *= y
is equivalent to
x = x * y
The difference is that x is calculated only once. The * operator is pre-defined for numeric types to perform multiplication operations.
The *= operator cannot be overloaded directly, but user-defined types can overload the * operator (see operator).

32,/=
The Division assignment operator.
Note:
An expression that uses the/= assignment operator, such as
X/= y
is equivalent to
× = x/y
The difference is that x is calculated only once. The/operator is pre-defined for numeric types to perform division operations.
The/= operator cannot be overloaded directly, but user-defined types can be overloaded/operators (see operator). For all compound assignment operators, an implicitly overloaded two-tuple operator overloads the equivalent compound assignment.

33,%=
The module assignment operator.
Note:
An expression that uses the%= assignment operator, such as
X%= y
is equivalent to
x = x% y
The difference is that x is calculated only once. The% operator is pre-defined for a numeric type to calculate the remainder after dividing.
The%= operator cannot be overloaded directly, but user-defined types can overload the% operator (see operators (C # Reference)).

34, &=
The "and" assignment operator.
Note:
An expression that uses the &= assignment operator, such as
X &= y
is equivalent to
x = x & y
The difference is that x is calculated only once. The & operator performs a bitwise logical AND operation on an integer operand and performs a logical AND operation on the bool operand.
The &= operator cannot be overloaded directly, but user-defined types can overload the $ two & operator (see operator).

35, |=
"or" assignment operator.
Note:
An expression that uses the |= assignment operator, such as
X |= y
is equivalent to
x = x | Y
The difference is that x is calculated only once. | operator performs a bitwise logical OR operation on an integral operand, performs a logical OR operation on the boolean operand.
The |= operator cannot be overloaded directly, but user-defined types can be overloaded | operator (see operator).

36, ^=
The XOR assignment operator.
Note:
Expressions in the following form
X ^= y
The following rules are calculated:
x = x ^ y
The difference is that x is calculated only once. The ^ operator performs a bitwise XOR operation on an integer operand and performs a logical "XOR" operation on the bool operand.
The ^= operator cannot be overloaded directly, but user-defined types can be overloaded! operator (see operator).

37, <<=
The left Shift assignment operator.
Note:
Expressions in the following form
X <<= y
The following rules are calculated:
x = x << y
The difference is that x is calculated only once. The << operator moves X to the left by the number of digits specified by Y.
The <<= operator cannot be overloaded directly, but user-defined types can overload the << operator (see operator).

38, >>=
Move the assignment operator right.
Note:
Expressions in the following form
X >>= y
The following rules are calculated:
x = x >> y
The difference is that x is calculated only once. The >> operator shifts the x right by the amount specified by Y.
The >>= operator cannot be overloaded directly, but user-defined types can overload the >> operator (see operator).

39,
The operator combines pointer dereference with member access.
Note:
Expressions in the following form
X->y
(where x is a pointer of type t*, and Y is a member of T) is equivalent to
(*x). Y
The-operator can only be used in unmanaged code.
The-operator cannot be overloaded.

Forty??
If?? The left operand of the operator is not NULL, and the operator returns the left operand, otherwise the right operand is returned.

Note:
A nullable type can contain a value, or it can be undefined.?? The operator defines the default value that is returned when a nullable type is assigned to a non-nullable type. If you do not use a nullable type when assigning it to a non-nullable type?? operator, a compile-time error is generated. If a cast is used and a nullable type is not currently defined, a InvalidOperationException exception occurs.

General description and function of C # operator Encyclopedia _ various operation symbols

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.