C # type conversion 20140815

Source: Internet
Author: User

I. type conversion

1. Forced conversion (display conversion)

A. Variable = (type to be converted) variable -- only suitable for conversion within the same type.

Example: float;

Double B = 3.14;

A = (float) B;

B. Variable = convert. To data type (variable) -- value type conversion.

PS: To data type must be represented by ". NET data type.

Example: float;

Double B = 3.14;

A = convert. tosingle (B );

C. Variable = data type. parse (variable) -- "()" can only be of the string type, and the string is parsed into the corresponding value type.

Example: float;

Double B = 3.14;

A = float. parse (B)

PS: only strings in "()" must conform to the type of the variable to be converted. Otherwise, no error is prompted during editing, but the variable cannot be run.

Example: String B = "hello ";

Int A = int. parse (B) -- cannot be converted to "int" because "hello ".

2. Automatic Computer conversion (implicit conversion)-the calculator can only perform operations on the same type of data.

Ii. Operators

1. mathematical operations

7 types: 1) + 2)-3) * 4)/5) % 6) + + 7 )--

4)/-- integer and integer calculation results are integer.

For example, int A = 10;

Int B = 3;

Console. writeline (a/B) -- the returned information is "3 ".

5) % -- Obtain the remainder, which must be used to control the data range.

For example, int A = 2357;

Int B = 36;

Console. writeline (a/B) -- the returned information is between 0 and 35. If no value is required, + 1 is allowed. The value range is from 1 to 36.

6) ++ auto-increment 7) -- auto-Increment

A. in a statement, only "+ +" "--" indicates that "+ +" -- "is the same as" + + "" -- "after a variable, no priority processing order.

For example, int A = 10;

A ++;

Console. writeline (a); -- the returned information is "11 ".

Int A = 10;

A --;

Console. writeline (a); -- the returned information is "9 ".

B. when there are other operations in a statement except "+ +" "--", "+ +" "--" before the variable takes precedence over the calculation "+ +" "--" data; variable "+ +" "--" final operation in calculation "+ +" "--" data.

For example, int A = 10, B;

B = A ++; -- B = A, A = a + 1.

Console. writeline (a); -- 11

Console. writeline (B); -- 10

For example, int A = 10, B;

B = ++ A; -- A = a + 1, B =.

Console. writeline ("A =" + a); -- A = 11

Console. writeline ("B =" + B); -- B = 11

For example, int A = 10, B;

B = A ++, A = B ++;

Console. writeline ("A =" + a); -- A = 12

Console. writeline ("B =" + B); -- B = 12

For example, int A = 10, B;

B = ++ A, A = B ++;

Console. writeline ("A =" + a); -- A = 12

Console. writeline ("B =" + B); -- B = 11

PS: "++" "--" is only applicable to variables, and "++" -- "can only be a variable, not a constant or a formula.

2. relational operation
6 in total: 1) = 2 )! = 3)> 4) <5)> = 6) <=

1) = -- judge

Example: int A = 3, B = 4, C = 5;

Console. writeline (A = B); -- bool type, indicates that a and B are equal? Is it true? -- The returned information is "false ".

Usage: used to determine whether the result is expected.

For example, console. Write ("enter an integer less than 100 :");

String S = console. Readline ();

Int num = convert. toint32 (s );

Console. writeline (Num % 7 = 0) -- is the remainder 0? Is it an integer? -- Is the value a multiple of 7?

Console. writeline (Num % 10 = 7) -- is the number of single digits 7?

Console. writeline (Num/10 = 7) -- is the ten-digit number 7?

PS: For all Relational operators, the returned information is of the bool type. If the expression is true, the return information is true. If the expression is not true, the return information is false.

3. logical operations

3 in total: 1) & 2) | 3 )!

1) & -- Logical and, indicating and.

A. True & True = true

B. False & True = false

C. False & false = false
Example: int A = 3, B = 4, C = 5;

Console. writeline (A> B & B <C); -- the returned information is "false ".

Example: int A = 3, B = 4, C = 5;

Console. writeline (! (A> B) & B <C); -- returns "true ".

2) | -- Logical or

A. True | true = true

B. False | true = true

C. False | false = false

PS: For all logical operators, the returned information is of the bool type. If the expression is true, the return information is true. If the expression is not true, the return information is false.

3 )! -- Non-logical, reverse.

4. Other operators

1) Value assignment operator: =

2) composite OPERATOR: = is assigned a. + = B.-= C. * = D./= E. % =

Example: int A = 5;

A + = 10; -- A = a + 10.

Console. writeline (a); -- the returned information is "15 ".

3) conditional OPERATOR: .? B .:

(Expression 1 )? (Expression 2) :( expression 3)

For example, console. writeline ("Enter the time in the 24-hour format :");

Int H = convert. toint32 (console. Readline ());
String ap = "";

AP = h> 12? "PM": "am"; -- is H greater than 12? It is "PM", not "am ".

H = h> 12? H-12: H; -- H greater than 12? If it is set to true, it is less than 12. If it is not true, It is H.

Iii. Operator priority

Mathematical operators are higher than Relational operators, and Relational operators are higher than logical operators.

1) Priority of mathematical operators: "+ +" "--" higher than "*" "/" "% ", "*"/"" % "is higher than" + ""-".

2) logic operator operation Priority: "&" higher than "| ".

PS: () is the highest priority calculation, followed !; "++" After the variable is the final operation, followed by =.

 

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.