Tutorial C # entry 5 data type conversion

Source: Internet
Author: User

In this lesson, we will achieve the following results:

Addition

Enter a value of 6
Enter 12 numbers
6 + 12 = 18
Press any key to continue...

 

According to the knowledge we learned in the previous lesson, when the user input 6, after reading the 6, the 6 is stored in the memory of the string type "6 ", if '+' is used to implement addition, the effect is actually "6" + "12" = "612 ", because '+' is used to concatenate two values of the string type, rather than the addition effect between values, therefore, we need to convert "6" or "12" to 6 or 12 of the numeric type, and then use the '+' sign to realize addition between values, C # There are two ways to convert data types:

    • Parse () method: converted type. parse (variable of the string type to be converted)
    • Convert class: Convert. To type after conversion (variable to be converted)

The following describes how to convert data types in the first method:

For example, string a1 = "6"; // defines a string type variable. The A1 value is "6"

The syntax for converting the int_a1 variable to the int type is: int int_a1 = int. parse (A1 );

The syntax of the float_a1 variable to be converted to the float type is: Float float_a1 = float. parse (A1 );

The syntax of the second conversion method is as follows:

Int int_a1 = convert. toint32 (A1 );

Float float_a1 = convert. tosingle (A1 );

The difference between the two is that the parse method can only convert string type variables to the desired type, while the convert method can convert any type to the desired type. The following is a question for students: How much is the value of Boolean variable B converted to double when the value of B is false?CodeHow to write? Which conversion method is used?

1 Bool B =   False ;
2 Double Double_ B = Convert. todouble (B );
3 Console. writeline (double_ B );

The answer is 0. If B = true, the answer is 1. The convert mode should be adopted, because the data type to be converted is a non-string bool type.

So please implement the effect of the course:

Addition
1 Console. Write ( " Enter a number " );
2 String String_a = Console. Readline ();
3 Int A =   Int . Parse (string_a );
4 Console. Write ( " Enter two numbers " );
5 Int B =   Int . Parse (console. Readline ());
6 Int Sum = A + B;
7 Console. writeline ( " {0} + {1 }={ 2} " , A, B, sum );

Please pay attention to writing 5th lines of code and learn the short-term usage. You must master the data type conversion and use it frequently, this is the end of this lesson. The next lesson will teach you how to use operators.

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.