data type
-
8 integers + 3 floating point types and 11 value types
-
bool type can only be true or false
Judgment conditions can only be judged by true or false conditions
-
sizeof() ` get data type length
//According to the byte type, open a 1-byte memory space in the memory, name this space byteValue, and convert 2 into binary data to fill this memory space //Byte: a byte, unsigned (the value starts from zero and cannot be negative) 0-255 byte byteValue = 2; //sbyte: one byte, signed data type - 2 ^ 7 ` - 2 ^ 7 - 1 - 128 - 127 sbyte sbyteVlaue; //short: two bytes, signed data type short shortVlaue; //ushort: two bytes, unsigned data type ushort ushortVlaue; //int: 4 bytes, signed int intValue; //uint: 4 bytes, signed uint uintValue; //Long: 8 bytes, signed long longValue; //ulong: 8 bytes, unsigned ulong ulongVlaue; //Do not use floating-point numbers for equality (= =) comparisons //If the size of two floating-point numbers is consistent, ABS (a-b) < 0.01 //float: four bytes float floatValue; //double: 8 bytes double doubleValue; //decimal:16 bytes decimal decimalValue; //char character type: two bytes. The content caused by single quotation marks is the character type char charValue = 'a'; //String string is caused by double quotes string stringVlaue = "12345"; //Bool: a byte, c# in which the value of bool type is only true or false bool boolValue = true; //sizeof is an operator used to obtain the length of the data type (the number of bytes occupied). The parentheses can only be filled in the data type Console.WriteLine(sizeof(int));
reflection:
1. How is the data stored in the computer?
According to the byte type, open a 1-byte memory space in the memory, name this space byteValue, and convert 2 into binary data to fill this memory space
Data type conversion
- The difference between implicit conversion and cast
- Points needing attention for conversion between types
- int.TryParse (string, out, int i) is true if the quasi exchange succeeds, and false if it fails
//Implicit conversion: from small value range to large value range int intValue1 = 10; long langValue1 = intValue1; //Cast: convert from large value range to small value //1. If the number before the forced conversion cannot be saved by the data type to be converted, it will be discarded to the first few binary numbers that cannot be saved //2. Any decimal number in c# is of double type. If you want to assign a decimal value to a float type variable, you must perform strong conversion (float is followed by an f suffix, decimal is followed by an m) //3.bool type cannot be converted with any data type in c# the only values are true and false //intVlaue = (int)longValue; float longValue2 = 12.2f; int intVlaue2 = (int)longValue2; //4. Data conversion of char type. Char type can be understood as a numeric type, but it will be converted into corresponding characters through the coding table when used //Therefore, char type can be converted to numeric type char charVlaue1 = 'a'; int intVlaue3 = charVlaue1; //The numeric type can be converted to a string by adding + "" or. tostring() //tostring can take a parameter. The parameter type is string. It is entered in the form of 0.00. If there are several zeros after the decimal point, several decimal places will be retained (rounded) //If it is written in the form of. 00, if the decimal integer part is 0, the integer will be discarded when output. If it is not 0, it will be output normally //The string can be converted to int type through the api int.parse (), but if the string is not in the form of a pure number, the conversion will report an error string stringVlaue2 = intVlaue2 + ""; stringVlaue2 = intVlaue2.ToString(); stringVlaue2 = "97"; intVlaue2 = int.Parse(stringVlaue2); floatValue = float.Parse(stringVlaue2); //int.TryParse string str = "96"; int res; Console.WriteLine(int.TryParse(str,out res)); Console.WriteLine(res);
Common conversion APIs:
. tostring: convert numeric type to string type
tostring can take a parameter. The parameter type is string. It is entered in the form of 0.00. If there are several zeros after the decimal point, several decimal places will be retained (rounded)
If it is written in the form of. 00, if the decimal integer part is 0, the integer will be discarded when output. If it is not 0, it will be output normally
. Parse: used to convert string type to any type (ensure data type compatibility)
Format: data type. Parse (value of string type)
Summary of data type conversion: //1.int to string int a; string b = a.ToString(); //2.string to byte [] //Method 1: string a; char[] b = a.ToCharArray(); byte[] c = new byte[b.Length]; for(int i = 0; i < b.Length; i++) { c[i] = (byte)b[i]; } //Method 2: //Using functions provided by the system string str; Byte[] bt = System.Text.Encoding.ASCII.GetBytes(str.ToCharArray()); //3.int to byte [] //Method 1: int i; byte[] temp = new byte[]; int pos; for(pos = 0; pos < 4; pos++) { temp[pos] = (byte)(i & Oxff); i >>= 8; if(i == 0) break; } //Method 2 //Functions provided by the system int i; byte[] data = new byte[4]; data = System.BitConvarter.GetBytes(i); //4.byte [] to string //Method 1: byte[] tmp; string str = new System.Text.ASCIIEncoding ().GetString (tmp); //Method 2: byte[] tmp; string str = System.Text.Encoding.ASCLL.GetString(tmp); //5.string to int //Method 1: string str; int i = Convert.ToInt32(str); //Method 2: string str; int i = int32.Parse(str); //6.byte [] to int //Method 1: int res = 0; int temp = 0; byte[] result; for(int i = 3; i >= 0; i--) { res<<= 8; temp = result[i] & 0xff; res |= temp; } //Method 2: byte[] result; int res = System.BitConverter.ToInt32(result,result The starting position you want to convert);
a key:
1.Convert method: the most flexible method in data type conversion. It can convert the value of any data type into the value of any data type
Data type variable name = convert.To Data type (variable name);
2.BitConverter class, Encoding class
Detailed reference:
https://www.pianshen.com/article/7250460709/
Supplement:
- var keyword
- dynamic variable type
- Placeholder usage {}
- Use of escape characters
- Nullable type
- How to indicate that a variable has no assignment
//In the process of program compilation, if the var keyword is encountered, the var keyword will be replaced with the corresponding data type according to the value after the equal sign //The efficiency of using var keyword is the same as that of directly using int and other data types, but the compilation efficiency will be slower //Reduced readability of code var num = 5; var value = "123"; //Dynamic variable type: when the program is running, the system reads the dynamic keyword and changes the data type according to the content of the value //Grammar sugar dynamic value1 = 5; value1 = "123"; //Placeholder {} Console.WriteLine(); //Escape character: add an @ before the string to indicate that all escape characters of the following string are invalid Console.WriteLine(@"D:\c#\#"); //nowrap Console.Write("nihao"); //Empty console Console.Clear(); //Blocking function string inputString = Console.ReadLine(); Console.WriteLine(inputString); //In c#, all value type variables cannot be directly assigned to null, and reference type variables can be copied to null //How to indicate that a variable has no assignment //1. Give an impossible value int age = -1; //2. Nullable type //Add a?, after the data type of a value type?, Means to change this type into an nullable type and add null to the original value range int? age1 = null; age = (int)age1; //?? The number after the symbol is a null value //age1 == null ? 1 : age1; age = age1 ?? 1; //The variable assigned to cannot be used, and the editor will report an error directly //In c#, all mathematical operations, if both operands are of integer type, the output result will also be of integer type int a = 1; int b = 4; Console.WriteLine((float)a / b);
Key points: (noteworthy points, which will be used in the future)
1. Add a @ in front of the string to indicate that all subsequent escape characters are invalid.
2. In c#, all value types cannot be directly assigned to null, but reference types can
3. To change the data of a value type into an nullable type is to add a question mark after this type
Error received
//In c#, all mathematical operations, if both operands are of integer type, the output result will also be of integer type int a = 1; int b = 4; Console.WriteLine((float)a / b);
Key points: (noteworthy points, which will be used in the future) 1.Prefix the string with a@Indicates that all subsequent escape characters are invalid. 2.stay c#In, all value types cannot be directly assigned null, but reference types can 3.To change the data of a value type into an nullable type is to add a question mark after this type 4.??After the symbol indicates if the number is null Value of