C#, introductory tutorial (08) - basic knowledge of basic data types and usage

Previous:

C#, introductory tutorial (07) - source files and directory structure of software projects icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/124139947

The data type is used to specify the storage type of the value of the data entity (DataEntity, including but not limited to attributes, variables, constants, and function return values ​​of classes or structures).

The C# language is a strongly typed language, so each data body must specify a data type.

The data types of C# language are divided into " value types " and " reference types ".

(1) Value types include: integer, floating point, character, Boolean, enumeration, etc.;

(2) Reference types include: structures, classes, interfaces, arrays, delegates, strings, etc.

The value type is stored in the memory stack, and each time the value is accessed, it will be operated in the memory; the reference type will first create a reference variable in the stack, then create the object itself in the heap, and then assign the first address of the memory where the object is located to the reference. variable.

Commonly used basic data types in the C# language include integers, floating point types, character types, and Boolean types among value types, as well as commonly used string types among reference types.

1. Integer type int

Integer type is the type that stores integers. According to different value ranges, the C# language divides integers into byte, short, int and long. Integers are further divided into signed integers and unsigned integers . Signed integers can represent negative numbers, while unsigned integers can only represent positive numbers.

The specific integer types and their representation ranges are shown in the following table.

type Ranges
sbyte Signed number, occupies 1 byte, -2 ^ 7~2 ^ 7-1
byte Unsigned number, occupies 1 byte, 0~2 ^ 8-1
short Signed number, occupies 2 bytes, -215~215-1
ushort Unsigned number, occupies 2 bytes, 0~2 ^ 16-1
int Signed number, occupies 4 bytes, -2^31~2^31-1
uint Unsigned number, occupies 4 bytes, 0~2 ^ 32-1
long Signed number, occupies 8 bytes, -2 ^ 63~2 ^ 63-1
head Unsigned number, occupies 8 bytes, 0~2 ^ 64-1

Int is commonly used in C# language . Application scenarios include:

// 常量
const int TWO = 2;

// 整型变量,以及初值
int a = 10;

// 整型数组
int[] array;

// 结构体成员
struct student {
    int Id;
}

// 类属性
class teacher {
    int Id { get; set; } = 0;
}

// 函数返回值
int Sum()
{
    return (3+4);
}

2. Real number double (floating point type)

Floating-point type refers to the decimal type. There are two types of floating-point types in the C# language, one is called single-precision floating-point type and the other is called double-precision floating-point type.

type Ranges
float Single precision floating point type, occupies 4 bytes, retains up to 7 decimal places
double Double precision floating point type, occupies 8 bytes, retains up to 16 decimal places


Double is commonly used in C# language  .

If you want to use single-precision floating point type, you need to add f or F after the value to express it, such as 123.45f, 123.45F.

// 常量
const double PI = 3.14159265359;

// 实数变量,以及初值
double b = 10;

// 实数数组
double[] array;

// 结构体成员
struct student {
    double Height;
}

// 类属性
class teacher {
    double Weight { get; set; } = 0;
}

// 函数返回值
double SQRT2()
{
    return System.Math.Sqrt(2.0);
}

3. Character type char and string type

1. Character type char

The character type is represented by the char keyword. Characters stored in the char type need to be enclosed in single quotes, such as 'a', '中', etc.
 

2. String string

The string type can store multiple characters. It is a reference type. The number of characters stored in the string type can be considered to be unlimited because the memory size used is not fixed but variable.

C# commonly used string type  string  . String type data must be enclosed in double quotes , such as "abc", "123", etc.

3. Special characters (escape characters)

There are also some special strings in the C# language, which represent different special functions. Since string type data needs to be enclosed in double quotes when declaring it, the double quotes become special characters and cannot be output directly. The function of the escape character is to output this character with special meaning.

Escape characters are very simple. Commonly used escape characters are shown in the table below.

escape character Equivalent characters
\' apostrophe
\" Double quotes
\\ backslash
\0 null
\a Warning (generates a beep)
\b backspace
\f Page change
\n newline
\r Enter
\t horizontal tab
\v vertical tab

Remember the four red escape characters above, they appear often.

// 常量
const string SEASONS = "春夏秋冬";

// 字符串变量,以及初值
string c = "hello world!";

// 字符串数组
string[] array;

// 结构体成员
struct student {
    string Name;
}

// 类属性
class teacher {
    string Name { get; set; } = String.Empty;
}

// 函数返回值
string WhoAreYou()
{
    return "DOGOD!";
}

4. Boolean type bool

 The C# language Boolean type is declared using bool , which has only two values, true and false .

Boolean values ​​are used for switch-type data and option settings.
Boolean values ​​are also often used in conditional judgment statements, for example, to judge the size of two values, etc.

 Application scenarios:

// 变量,初值
bool Open = false;
bool Visible = true;

// 逻辑
bool OK = (1>0);
bool IDONTKNOW = (a == b);

have a good weekend!

Next:

C#, introductory tutorial (09) - basic knowledge of operators icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/123908269

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/123906998