C # explicit and implicit Conversions

Source: Internet
Author: User

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace CSHARP basics <br/>{< br/> class type conversion <br/>{< br/> Public static void main () <br/>{< br/> // byte b1 = 22; <br/> // byte b2 = 33; <br/> // in the following sentence, by default, B1 + B2 returns an int, But we declare it as byte, which results in loss of bytes. Therefore, the system <br/> // does not implicitly convert to generate compilation errors. <br/> // byte Total = b1 + B2; <br/> // if we use long to store results, data will not be lost, some systems have implemented an implicit conversion <br />// Long Total = b1 + B2; <br/> // do not implicitly convert the null type to the normal type. This is because null processing is available, so normal values cannot be processed null <br/> // Int? A = 22; <br/> // int B = A; error <br/> // can be implicitly converted to an empty type, the rule is consistent with the normal type <br/> // Int? A = 99; <br/> // long? B = A; <br/> // Int? A = 255; <br/> // byte? B = A; error, although 255 is indeed within the acceptable range of byte </P> <p> console. writeline ("------------ explicit type conversion ---------------------"); <br/> // long a = 2000000000000; <br/> // int B = (INT); <br/> // console. writeline (B); <br/> // In the code above, converting long to int will discard several of the bytes, and the value of Long is exactly greater than the table range of Int, so the last value <br/> // is not what we want </P> <p> // long a2 = 65535; <br/> // int b2 = (INT) A2; <br/> // console. writeline (B2); <br/> // the above Code is also a display conversion, while 65535 is within the int byte range Even after the bytes are lost, the value remains unchanged, which is the expected result </P> <p> // consider a problem. Are there any luck in the two methods above, so how can we control it if it is out of scope? <Br/> // C # overflow of the checked check value </P> <p> // long a3 = 20000000000000000; <br/> // int B3 = checked (INT) A3 ); // an exception occurs during running. </P> <p> // the fractional part is lost when the decimal point is converted into an integer. <br/> // float price = 23.5f; <br/> // int reint = (INT) (Price + 0.5f); <br/> // The method above plus 0.5f is used to bring the nearest integer to the rounding price, it is generally used to not participate in operations. It is only used for demonstration and is recommended! </P> <p> // console. writeline (reint); </P> <p> // analyze the string and convert the value <br/> // string STR = "65535 "; <br/> // int I = int. parse (STR); <br/> // console. writeline (I); </P> <p> // string str2 = "false"; <br/> // bool I2 = bool. parse (str2); <br/> // console. writeline (I2); </P> <p> console. read (); </P> <p >}< br/>}</P> <p>

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.