10-10 C # Basic data type conversion (master)
First lesson the conversion between data types
Conversions for basic types: auto-convert (implicit conversion) and cast (show transformations)
Boxing conversions: Allow value types to be implicitly converted to reference types.
Unboxing conversions: Allow conversion of reference type display to value type
There are 2 steps: First, check if the object is outside the type range, and then convert.
object contains all types, other types can be converted to object types, but object cannot be converted to other types.
Example: int i=0
Object o = i;
Three modes of conversion:
1), with (). () is the converted data type and can only be converted to the same large class (conversion between value types or conversion between reference types). Forced conversions
Example 1:int i = 0;
Double d = (double) i;
Console.Write (d);
Console.ReadLine ();
Example 2:int i = 0;
Double d=i;
D = 1.23;
i= (int) D;
Console.Write (i);
Console.ReadLine ();
After execution:
Example 3, float f = 1.23f;
int i = f;
Console.Write (i);
Console.ReadLine (); This is the wrong wording.
When the writing console program finishes starting, the above content appears, indicating that floating-point types cannot be automatically (implicitly) converted to shaping, and there must be a casting process in between.
Write correctly:
float F = 1.23f;
int i = convert.toint16 (f);
Console.Write (i);
Console.ReadLine ();
After execution:
2), use CONVERT (a class that converts a basic data type to another base data type). Convert can be converted only if it conforms to the range of data types. Forced conversions
Example 1:float f=1.3f; When you create a float type, you add an F suffix.
Double d=convert.todouble (f);
Console.Write (d);
Console.ReadLine ();
After execution:
Example 2, string s = "12";
int i = convert.toint16 (s);
Console.Write (i);
Console.ReadLine ();
After execution:
3),. Parse Parse. Used for string type to go to the corresponding value type.
4) Example: string s = "12.2";
Double d = Double. Parse (s);
After execution: (after executing with parse, the values are unchanged.) ) Implicit conversion
However, the string type cannot be converted directly to type int and must be re-cast.
Example: string s = "12.2";
Double d = Double. Parse (s);
int i= (int) D;
Console.Write (i);
Console.ReadLine ();
After execution:
Correct writing process: float A;
String b = "3.14";
A = float. Parse (b);
int i = (int) A;
Console.Write (i);
Console.ReadLine ();
After execution:
Exercises: Judge: Console.WriteLine ("Please enter an integer within 100:37");
int a = convert.toint16 (37);
Console.WriteLine (a% 7 = = 0);//a can be divisible by 7
Console.WriteLine ();
Console.WriteLine (A% = = 7);//a is the digit 7
Console.WriteLine ();
Console.WriteLine (A/10 = = 7); Whether the 10 bits of//a are 7
Console.ReadLine ();
After execution, the interface appears:
Follow-up exercises:
Design a small program: Enter the number of integers in a row, enter a value for each line, enter the tenth number of times, automatically calculate the amount of the and. (Any input, each execution, manual entry of different numbers, you can get different results.) )
Operating interface:
Post-Execution Display interface:
Add: A placeholder
String xingming= "Xiao Ming";
String xingbie= "male";
Console.Write ("Your name is: {0}, gender {1}", Xingming,xingbie);
Console.ReadLine ();
Post-Execution Display interface:
10-10c# the conversion between the underlying---data types