Operator
Arithmetic Operator +-*/%
Logical operators ~ & |!
String concatenation operator +
Increment and Decrement Operators ++ --
Shift Operator <>
Comparison operator =! = <> <=> =
Value assignment operator = + =-= * =/= % = <=^= <=> =
Member access operator.
Index operator []
Type conversion operator ()
Conditional operator (ternary operator )? :
Delegate join and delete operators +-
Object creation operator new
Type Information operator sizeof is typeof
Overflow exception control operator checked unchecked
Indirect addressing operator []
Namespace alias qualifier ::
Null merge operator ??
Simplified value assignment operator
x++,++x x=x+1x--,--x x=x-1x+=y x=x+yx-=y x=x-yx*=y x=x*yx/=y x=x/yx%=y x=x%yx>>=y x=x>>yx<<=y x=x<<yx&=y x=x&yx|=y x=x|y
int x=5;if(++x == 6)//true ----x is incremented to 6 before the evaluation{ Console.WriteLine("This will execute");}if(x++ == 7)//false ----x is incremented to 7 after the evaluation{ Console.WriteLine("This won‘t");}
When the code block is marked as checked, the CLR will execute the overflow check. If overflow occurs, an overflowexception exception will be thrown.
byte b = 255;checked{ b++;}Console.WriteLine(b.ToString());
Prohibit overflow check, marked as unchecked
The is operator checks whether the object is compatible with a specific type.
int i = 10;if (i is object){ Console.WriteLine("i" is an object");}
The as operator is used to convert the display type 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.
object o1 = "Some String";object o2 = 5;string s1 = o1 as string;//s1 = "Some String"string s2 = o2 as string;//s2 = null;
The sizeof operator can determine the length (in bytes) required for the stack value type)
Console.WriteLine(sizeof(int));
The typeof operator returns a system. Type object of a specific type.
Null merge operator ?? A shortcut is provided to indicate the possible null values when processing the null and reference types.
In a responsible expression, the operator priority should be avoided to generate correct results. Using parentheses to specify the execution sequence of operators can clean the code and avoid potential conflicts.
Implicit conversion
As long as the value does not change, the type conversion can be performed automatically (implicitly ).
Display Conversion
Type forced conversion
long val = 30000;int i = (int)val; //A valid cast.The maximum int is 2147483647
All predefined value types support the parse () method.
Binning and unboxing can convert the value type to the reference type and convert the reference type to the value type.
Equality of reference types: referenceequals () and two versions of equals (), comparison operator (=)
When the comparison value type is equal, use the same rule as the reference type: referenceequals () is used to compare references, equals () is used to compare values, and comparison operators can be considered as an intermediate item
Operator overload
The declaration method of Operator Overloading is the same as that of method, but the operator keyword tells the editor that it is actually a custom operator overload, followed by the actual symbols of related operators.
C # requires that all Operator Overloading be declared as public and static
Struct vector {public Double X, Y, Z; Public vector (Double X, doubley, Double Z) {This. X = x; this. y = y; this. z = z;} public vector (vector RHs) {x = RHS. x; y = RHS. y; Z = RHS. z;} public override string tostring () {return "(" + x + "," + Y + "," + Z + ")";} public static vector operator + (vector LHS, vector RHs) {vector result = new vector (LHS); result. X + = RHS. x; result. Y + = RHS. y; result. Z + = RHS. z; return result;} // test the static void main () {vector vect1, vect2, vect3; vect1 = new vector (3.0, 3.0, 1.0 ); vect2 = new vector (2.0,-4.0,-4.0); vect3 = vect1 + vect2; console. writeline ("vect1 =" + vect1.tostring (); console. writeline ("vect2 =" + vect2.tostring (); console. writeline ("vect3 =" + vect3.tostring ());}
C # language requires a pair of reload comparison operators. That is, if "=" is reloaded, "!" must be reloaded. ="
User-Defined forced type conversion
C # two types of forced conversions are allowed: implicit and explicit forced conversions.
Both public and static types must be declared for forced conversions.
Struct currency {public uint dollars; Public ushort cents; public currency (uint dollars, ushort cents) {This. dollars = dollars; this. cents = cents;} public override string tostring () {return string. format ("$ {0 }. {1,-2: 00} ", dollars, cents);} // implicit public static implicit operator float (currency value) {return value. dollars + (value. cents/100366f);} // explicitly public static explicit operator currency (float value) {checked {uint dollars = (uint) value; ushort cents = (ushort) (value-dollars) * 100); return new currency (dollars, cents );}}}
It is completely legal to define forced conversions between instances of different structures or classes.But there are two restrictions:
. If a class is derived from another class, you cannot define forced conversions between these classes (these types already exist)
Type forced conversion must be defined inside the source or target data type
Forced conversion of packing and unpacking Data Types
Forced conversion of multiple types
C # learning notes ---- operator and forced type conversion