Nullable type, C # overflow check, type of operator and Operator Overloading

Source: Internet
Author: User

1) nullable type

A nullable type is the synthesis type of the basic type plus "whether it is a Null Indicator. For a type, if you can either assign a value or assign a null reference to it (indicating that there is no value), we can say that this type can be empty. Therefore, an empty type can indicate a value, or a value does not exist. For example, the reference type similar to string is the void type, while the value type similar to int32 is not the void type. Because the capacity of the value type is sufficient to indicate a value suitable for this type, it cannot be empty.

Some people think that if the value of int type variable is 0, it indicates null. This is incorrect. 0 is also its value and does not indicate null.

See the following code Demonstration:

1 static void main (string [] ARGs) 2 {3 // int n = NULL; 4 // when the int type is directly assigned null, the compiler reports the error "cannot convert the null type to the int type because it is a type that cannot be considered null" 5 Int? Onevalue = NULL; 6 // The nullable type is very similar to the non-nullable type. The key is the modifier "?" after the type. 7 // Int? Onevalue = 10; 8 If (onevalue. hasvalue) // use the nullable type hasvalue attribute to determine whether a value is stored 9 {10 // int nnum = onevalue; 11 // if you try to obtain the onevalue value, the compiler reports 12 console errors. writeline (onevalue. value); 13} 14 else15 {16 console. writeline ("onevalue value is blank! "); 17} 18}

2) C # overflow check

When one integer is converted to another, the process depends on the overflow check context. The checked keyword is used to enable overflow checks for integer arithmetic operations and conversions explicitly, while the unchecked keyword is used to cancel overflow checks for integer arithmetic operations and conversions.

① Enable overflow check: when the value of the operand is within the value range of the target type, the conversion is successful. Otherwise, an exception will be thrown.

See the following code:

1 class typeconvert 2 {3 static void main (string [] ARGs) 4 {5 typeconvert = new typeconvert (); 6 typeconvert. dosomething (); 7} 8 9 Public void dosomething () 10 {11 // The Myint value is 214748364712 try13 {14 int Myint = int. maxvalue; 15 byte mybyte = checked (byte) Myint); 16} 17 catch (overflowexception) 18 {19 throw; 20} 21} 22}

In the code, the maximum value of Myint is 2147483647. After the value is forcibly converted to the byte type, because the value range of byte is 0-255, the checked keyword is used for overflow check, here, the byte mybyte type cannot accommodate much larger than its capacity value and throws an exception.

② Cancel the overflow check

During the type conversion process, do not check whether the data exceeds the maximum value of the target data type, which means that the type conversion is always successful. If the value range of the source data type is greater than the value range of the target data type, the excess part will be truncated. If the value range of the source data is smaller than that of the target data type, after conversion, it will be filled with symbols or zero to the same size as the target type. If it is equal to, it will be directly converted to the target type.

The instance code is as follows:

1 1 class typeconvert 2 2 {3 3 static void main (string [] ARGs) 4 4 {5 5 typeconvert = new typeconvert (); 6 6 typeconvert. dosomething (); 7 7} 8 8 9 9 Public void dosomething () 10 10 {11 11 // The value of Myint is 214748364712 12 try13 13 {14 14 int Myint = int. maxvalue; 15 15 // byte mybyte = unchecked (byte) Myint; 16 16 // This write method is the same as the following statement. The difference is that the keyword 17 17 byte mybyte = (byte) myint; 18 18 // No overflow check is used here, and the results will not throw an exception . However, the conversion result is incorrect. The maximum value of the byte type is 255, which is quite different from the original value. 19 19} 20 catch (overflowexception) 21 21 {22 throw; 23 23} 24 24} 25 25}

③ Typeof Operator

Typeof is a unary operator used to return information of any type.
The syntax of the typeof operator is as follows: Type type = typeof (type );
The sample code is as follows:

1 class Program 2 {3 static void main (string [] ARGs) 4 {5 type T = typeof (Program); 6 console. writeline ("method:"); 7 8 methodinfo [] methodinfo = T. getmethods (); 9 // return all public methods of the type 10 foreach (methodinfo minfo in methodinfo) 11 {12 console. writeline (minfo. tostring (); 13} 14 15 console. writeline ("member:"); 16 memberinfo [] memberinfo = T. getmembers (); 17 // return all public members of the type 18 foreach (memberinfo minfo in methodinfo) 19 {20 console. writeline (minfo. tostring (); 21} 22 23 console. readkey (); 24} 25}

The running result is as follows:

④ Operator Overloading
Operator overloading can only be applied to classes or structures. To overload an operator, you can declare a method named operator X and implement it, such as operator + and operator-. The unary and binary operators can both be overloaded. Correspondingly, the overload method includes one or two parameters (class or structure type). The syntax is as follows:
// Unary operator overload
Public static operator of the custom type to be reloaded (type)
// Overload of binary Operators
Public static operator of the custom type to be reloaded (type 1, type 2)

Note: The reloads of all operators are static and must be modified using public.
The sample code is as follows:

1 class Program 2 {3 Public int value {Get; set;} 4 5 static void main (string [] ARGs) 6 {7 program O1 = new program (); 8 o1.value = 10; 9 program O2 = new program (); 10 o2.value = 20; 11 12 program O3 = O1 + O2; 13 console. writeline (o3.value); 14 console. readkey (); 15} 16 17 // <summary> 18 // here, the "add (+)" operator is overloaded for the program class to add two objects, the overload of operators improves programming convenience 19 // </Summary> 20 // <Param name = "O1"> </param> 21 /// <Param name = "O2"> </param> 22 // <returns> </returns> 23 public static program operator + (Program O1, program O2) 24 {25 program o = new program (); 26 o. value = o1.value + o2.value; 27 return O; 28} 29}

Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held.

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.