Conditional operator? : Condition? Real Value: false value
C # provides the checked and ucchecked operators. If a code block is marked as checked, CLR will execute the overflow check. If a code block overflows, an overflowexception exception will be thrown.
byte b = 255; checked { b++; } Console.WriteLine(b.ToString());
When the code is executed, an overflowexception exception is thrown.
byte b = 255; unchecked { b++; } Console.WriteLine(b.ToString());
The execution will print 0.
Is operator to check whether the type is of the specified type. This operator is very practical. In many cases, we need to use objects for value passing and generic processing. This is especially useful at this time. We can first determine whether the object is of the type we want. If the object is to be processed, we can also manually throw an exception when performing operations. This is very effective for development or data security processing. I remember I was asked a question during my previous interview: How can I test the value of an object? I think you should also think of comparing object values. This is not an error, but it is still not good enough. We should first determine whether it is the type we want, if not directly return false. Is In The compare value, this will not ensure that there will be no running errors, but also improve the performance.
object o = "111"; Console.WriteLine(o is string); //true object o2 = 111; Console.WriteLine(o2 is string); //false
The as operator performs explicit conversion of the reference type. If the type is not specified, null is returned.
object o = "111"; string s = o as string; //1111 object o2 = 111; string s2 = o2 as string; //null
Null merge operator (??)
Int? A = NULL; int B; B = ?? 10; // The value of B is 10 A = 3; B = ?? 10; // The value of B is 2
Comparison
Many know equals and =, but the object comparison also has a static comparison class system. Object. referenceequals.
1. In system. Object. referenceequals msdn, refer to determine whether the specified object instance is the same instance.
2. The equals () method reference in msdn:
By default, equals only supports equal references, but the derived class can override this method to support equal values.
For reference types, the definitions of equality are equal to objects, that is, whether these references reference the same object. For value types, equal is defined as equal by bit
3.
= Description of the operator in msdn:
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.
int inF = 1; bool a = System.Object.ReferenceEquals(inF, 1); Console.WriteLine(a); // false Console.WriteLine(inF.Equals(1));//true Console.WriteLine(inF == 1);//true