Detailed explanation of C# basic data types

Abstract: No matter what programming method or architecture is used, various basic data types are inseparable, and the selection of suitable data types affects our realization of programming methods and architectures. Therefore, it is crucial to have a deep understanding of C#'s basic data types.

Programming language: C#

Programming environment: Visual Studio 2019

Table of contents

integer type

 floating point type

Boolean type

character data type 

summary 

each message


        C# basic data types include integer type, floating point type, Boolean type and character data type. Without further ado, let's go straight to the dry goods in the following subsections.

integer type

        The integer type indicates that there are only integers in the data, excluding decimals, and can be divided into signed integers and unsigned integers according to whether they are signed or not. For signed integers and unsigned integers, they are divided into various integer types according to different storage capacities, as shown in the following table.

signed integer type
type of data Category Bytes Value range
sbyte (byte) System.Sbyte 1 -128~127
short (short integer) System.Int16 2 -32768~32767
int (integer) System.Int32 4 -2147483648~2147483647
long (long integer) System.Int64 8 -9223372036854775808~9223372036854775807
unsigned integer type
type of data Category Bytes Value range
byte (unsigned byte) System.Byte 1 0~255
ushort (unsigned short integer) System.UInt16 2 0~65535
uint (unsigned integer) System.UInt32 4 0~4294967295
ulong (unsigned long integer) System.UInt64 8 0~18446744073709551616

        When programming, it is often necessary to select the appropriate data type according to different capacities. For example, 500ml green tea will not be filled in a 1000ml cup, which will be wasteful, and it will not be filled in a 300ml cup, which will not fit, which corresponds to resource waste and data overflow in programming . In Visual Studio 2019, if overflow data is defined, there will be a corresponding error message.

 

         When defining an integer type, the decimal number is used by default. If you want to define a binary number, you need to add 0b in front; if you want to define a hexadecimal number, you need to add 0x in front.

 floating point type

        The floating-point number type represents data that includes an integer part and a decimal part. According to different precisions, it is divided into float (floating-point number), double (double-precision floating-point number) and decimal (high-precision floating-point number), as shown in the following table.

floating point type
type of data Category Bytes precision
float System.Single 4 7 digits
double System.Double 8 15 bit~16 bit
decimal System.Decimal 16 28~29 bits

       The default floating-point type of the system is double. If you want to define data of type float or decimal, you need to add postfix characters F, f or M, m after the data.

        

 

        Let's demonstrate the accuracy of the three types of floating-point numbers, and type in the following code.

float A1 = 520.1314131413141314F;
double A2 = 520.1314131413141314;
decimal A3 = 520.1314131413141314M;
Console.WriteLine(A1);
Console.WriteLine(A2);
Console.WriteLine(A3);

        The running results are as follows.

        It can be seen that the precision of the three types of floating-point numbers is different, and the number of decimal places reserved is different. Float and double have rounding errors for this data. Therefore, for this data, the decimal type is the most accurate.

Boolean type

        The Boolean type is represented by the keyword bool, and its class is System.Boolean, which occupies one byte, and its value is true or false, which is used to represent two states of logical true and logical false. It is worth noting that the Boolean value in C# cannot use 1 to represent true and 0 to represent false, as in C++, and only return the result of the operation as true or false.

character data type 

        The character data type is represented by the keyword char, and its class is System.Char. A character occupies two bytes, and each character corresponds to a Unicode code, that is, an ASCII code. The code range is 0~65535. Enter the following code, and the demonstration is as follows .

char ch1 = 'B';//定义字符使用单引号
char ch2 = '\x0042';//使用十六进制编码来表示字符B
Console.WriteLine(ch1);
Console.WriteLine(ch2);

        The running results are as follows.

        It can be seen that you can directly type in characters or use ASCII codes to represent characters. Sometimes you will use characters and ASCII codes for conversion. The correspondence between commonly used characters and ASCII codes is as follows.

summary 

        Basic data types are like small building blocks one by one. All advanced data types, and even the entire project project, are built one by one by them. Mastering these four basic data types and correctly selecting the most suitable data type can reduce memory consumption and prevent the risk of overflow.

each message

        The tree that embraces is born at the end of the hair; the nine-story platform starts from the pile of soil; the journey of a thousand miles begins with a single step.

 

Guess you like

Origin blog.csdn.net/lucgh/article/details/130466295