Data type
I. Basic data types
1. Value type ( NOT null)
⑴ integral type (can be negative)
Byle,int,short,long (from small to large)
Common integral type
int a= value (less than 10 digits)
Long b= value (more than 10 digits)
⑵ floating-point type ( can be negative and integer )
float f=25.6f; (uppercase and lowercase, if an integer can be removed f)
Double d=25.6;
Decimal dm=25.6m; (both uppercase and lowercase)
Common floating-point type
Double,decimal
2. Boolean type
BOOL type can only be true or false
BOOL Ok=true/false;
3. Character type
Char (only one character, length must be 1) can be a number, letter, text, symbol
Char c= ' ★ '; (single quote required)
Ii. type of reference
1. String
String can be null
2. Time-Date datetime
DateTime dt=new datetime (int year, int month, int day);
DateTime riqi=new datetime (2017,2,19);
Datetme Now=datetime.now; (Show system Current time)
Data conversion
1. Implicit conversion (boxing)
Change the value type to a reference type, and do not declare the conversion mode;
string " Ten " ; int - ; string c = a + b; Console.Write (c); Console.ReadLine ();
The display is 1020;
String c=a is not valid and cannot be converted directly;
The addition of other types to string types is assimilated by string;
2. Explicit conversions, also known as casts (unboxing)
1. Use the parse;
The target type. Parse (variable/value);
String a = "10";
int B=int. Parse (a);
Console.ReadLine ();
2. Use ();
(target type) variable/value
3. Using Convert; (recommended)
Convert.to target Type (variable/value)
string a ="ten"; int b =convert. ToInt32 (a); Console.ReadLine ();
Convert.tobyte (a);
Convert.tosingle (a);
Convert.todecimal (a);
Convert.todouble (a);
Convert.toboolean (a);
Convert.todatetime (a);
Other types are converted to String types: variable/value. ToString ();
Escape character: \
\ can cancel the special meaning of the next symbol
\ ' single quotation mark \ ' quotation mark \ \ Backslash \a warning (generates beep) \b Backspace \f page break \ n line break \ r enter \ t horizontal tab \v vertical tab
Constants: const can only be assigned one time
Keyword before variable name (cannot be assigned, only value)
Exercises
Console.Write ("Please enter the first Student's name:"); stringName1 =Console.ReadLine (); Console.Write ("Please enter the age of the first student:"); stringAge1 =Console.ReadLine (); Console.Write ("Please enter the first Student's language score:"); stringChinese1 =Console.ReadLine (); Console.Write ("Please enter your first Student's math score:"); stringMath1 =Console.ReadLine (); Console.Write ("Please enter the first Student's English score:"); stringENGLISH1 =Console.ReadLine (); Console.WriteLine ("--------------------------------------------------"); Console.Write ("Please enter a second student's name:"); stringName2 =Console.ReadLine (); Console.Write ("Please enter a second student's age:"); stringAge2 =Console.ReadLine (); Console.Write ("Please enter a second student's language score:"); stringChinese2 =Console.ReadLine (); Console.Write ("Please enter a second student's math score:"); stringMath2 =Console.ReadLine (); Console.Write ("Please enter the second student's English score:"); stringENGLISH2 =Console.ReadLine (); Console.WriteLine ("---------------------------------------------------"); Console.Write ("Please enter a third student's name:"); stringName3 =Console.ReadLine (); Console.Write ("Please enter a third student's age:"); stringAge3 =Console.ReadLine (); Console.Write ("Please enter a third student's language score:"); stringCHINESE3 =Console.ReadLine (); Console.Write ("Please enter a third student's math score:"); stringMath3 =Console.ReadLine (); Console.Write ("Please enter the third student's English score:"); stringENGLISH3 =Console.ReadLine (); Console.WriteLine ("-------------------results show------------------------"); DoubleFirst = convert.todouble (chinese1) +convert.todouble (math1) +convert.todouble (ENGLISH1); Console.WriteLine (name1+"The language score is: ""+chinese1+"", the math score is:""+math1+"", the English score is:""+english1+"", the total score is:""+first+"". "); DoubleSecond=convert.todouble (CHINESE2) +convert.todouble (math2) +convert.todouble (ENGLISH2); Console.WriteLine (name2+"The language score is: ""+chinese2+"", the math score is:""+math2+"", the English score is:""+english2+"", the total score is:""+second+"". "); DoubleThird=convert.todouble (CHINESE3) +convert.todouble (MATH3) +convert.todouble (ENGLISH3); Console.WriteLine (Name3+"The language score is: ""+chinese3+"", the math score is:""+math3+"", the English score is:""+english3+"", the total score is:""+third+"". "); Double Yuwen=convert.todouble (CHINESE1) +convert.todouble (CHINESE2) +convert.todouble (CHINESE3); Doubleyw=convert.todouble (Yuwen); Console.WriteLine ("The language score is ""+ Yuwen +"", the average score is""+yw/3+"". "); DoubleShuxue=convert.todouble (math1) +convert.todouble (math2) +convert.todouble (MATH3); Doublesx=convert.todouble (Shuxue); Console.WriteLine ("The math score is ""+shuxue+"", the average score is""+sx/3+"". "); DoubleYingyu=convert.todouble (ENGLISH1) +convert.todouble (ENGLISH2) +convert.todouble (ENGLISH3); Doubleyy=convert.todouble (Yingyu); Console.WriteLine ("The total English score is ""+yingyu+"", the average score is""+yy/3+"". "); Console.WriteLine ("The sum of all the account scores for all people is ""+ (Yuwen+shuxue+yingyu) +"". "); Console.ReadLine ();
"2017-2-19" C # data type, data conversion, variables, constants, escape characters