C base operator

  • 2020-05-19 05:36:54
  • OfStack

1. Conditional operator
The conditional operator (? :) also known as the 3 yuan (mesh) operator, is if... A simplified form of the else structure that can be nested.

int x = 1;  
string s = x + ""; ;  
s += (x == 1 ? "man" : "men");  
Console.WriteLine(s);// The output 1man  

2. checked and unchecked
 
byte b = 255;  
{  
    b++;  
}  
Console.WriteLine(b.ToString());// The output 0  

But since byte can only contain Numbers from 0 to 255, ++ will cause b to overflow. Therefore, if you mark a block of code as checked, CLR performs overflow checking, and if an overflow occurs, an OverflowException exception is thrown.
As follows:
 
byte b = 255;  
checked  
{  
    b++;  
}  
Console.WriteLine(b.ToString());// throw OverflowException Exception, arithmetic operation causes overflow   

If you want to prevent overflow checking, you can mark it as unchecked:

byte b = 255;  
unchecked  
{  
    b++;  
}  
Console.WriteLine(b.ToString());// The output 0 , do not throw an exception   

3, is
The is operator checks whether an object is compatible with a particular type. "Compatible" means that the object is of or derived from that type.
 
string i = "hello i...";  
if (i is object)  
{  
    Console.WriteLine("i is an object...");// The words were executed   
}  

4, as
The as operator is used to perform an explicit type conversion of a reference type (string is the reference type). If the type to be converted is compatible with the specified type, the conversion will succeed. If the type is not compatible, the as operator returns Null.
 
string i = "hello i...";  
if (i is object)  
{  
    object obj = i as object;// Explicit type conversion   
    Console.WriteLine(obj is string ? "obj is string..." : "obj is not string...");// The output obj is string...  
} 

5, sizeof
The sizeof operator determines the length (in bytes) required for the value type in stack:
 
int byteSize = sizeof(byte);// The output 1  
int charSize = sizeof(char);// The output 2  
int uintSize = sizeof(uint);// The output 4  
int intSize = sizeof(int);// The output 4  

6, typeof
The typeof operator is often used in conjunction with the GetType() method to reflect class properties, methods, and so on.

Type intType = typeof(int);  
System.Reflection.MethodInfo[] methodInfo = intType.GetMethods();  
methodInfo.ToList().ForEach(x => Console.WriteLine(x.Name));// reflecting int The method name of the type   

7. Nullable types and operators
If one or both operands are null, the result is null, as follows:

int? a = null;  
int? b = a + 4;//b = null  
int? c = a * 5;//c = null  

But when comparing nullable types, as long as one operand is null, the result of the comparison is false. But just because a condition is false does not mean that the opposite of that condition is true. Such as:
  
int? a = null;  
int? b = -5;  
if (a >= b)  
    Console.WriteLine("a > = b");  
else  
    Console.WriteLine("a < b");// It will output this sentence 

8. Null merge operator
Such as:
 
byte b = 255;  
{  
    b++;  
}  
Console.WriteLine(b.ToString());// The output 0  
0

Related articles: