C # type conversion (non-original)

Source: Internet
Author: User
1. forced conversion E. g. (INT) table1.rows ["ID"] [0]; this type of conversion is not recommended in programming. It is a packing and unpacking process, that is to say, the type to be converted must be the type to be converted; otherwise, errors will inevitably occur; E. g.: Object objtest = new newtype (); newtype newvalue = (newtype) objtest; 2. convert supports conversion between different types. However, the conversion type must be the type to be converted; otherwise, an error occurs. g.: convert. toint32 (table1.rows ["ID"] [0]); 3. datatype. parse () Note: The preceding three methods are insecure and need to be protected using try-catch statements. 4. datatype. tryparse (). If the conversion fails, the initial value is directly assigned to the target variable. E. g.: decimal P1; decimal. tryparse ("{}", out P1); 5. ASE. g.: Object objtest = new newtype (); newtype newvalue = objtest as newtype; for the as operator, it does not perform conversion operations, when the type of the object to be converted belongs to the conversion target type or the derived type of the conversion target type, the conversion operation can be successful without generating a new object. Therefore, it is safe to use as for type conversion. Why is the efficiency of type conversion using the as operator higher than that of the old-fashioned type conversion, because the conversion using the as operator is as mentioned above. First, judge the type of the current object, the type is converted only when the requirements are met. The traditional type conversion method uses the current object for direct conversion. To ensure successful conversion, add try-catch, this determines that its efficiency will not be higher than the efficiency of the as operator. Note that, no matter whether the traditional method or the as operator is used for type conversion, You need to determine whether the conversion is successful before use, as follows: If (newvalue! = NULL) {// work with the object named "newvalue"} note: the use of the as operator has the following restrictions. (1) There is no need to convert the types between types, that is, the compilation error will occur in the following compiling. Newtype newvalue = new newtype (); newtype1 newvalue = newvalue as newtype1; (2) it cannot be applied to value type data, that is, it cannot be written as follows (compilation errors may also occur ). Object objtest = 11; int nvalue = objtest as int; for the first point, the traditional type conversion method can be used, but the following method cannot be used. Newtypeone newtestone = new newtypeone (); newtypetwo newtesttwo = (newtypetwo) newtestone, add the type conversion operator function to the original type, that is, you need to complete code similar to the following. Public class newtypeone {public static explicit operator newtypetwo (newtypeone objtest) {// convert object into new type} For the second vertex, the is operator can be used in C, in addition to the old type conversion operation, you can safely complete the conversion. To complete the above operation, the correct syntax is as follows. Object objtest = 11; If (objtest is int) {int nvalue = (INT) objtest ;}

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.