One, type
1. C # programs are a set of type declarations .
2. An object created by instantiating a type is called an instance of an object or type of type.
3. C # provides 16 predefined types, including 13 simple types and 3 non-simple types.
The ①13 simple types include 11 numeric types , including integer and floating-point types, and 2 non-numeric types .
Table 111 types of numeric values
Type name |
Comments |
Integer type |
8 kinds |
SByte |
8-bit signed integer |
Byte |
8-bit unsigned integer |
Short |
16-bit signed integer |
UShort |
16-bit unsigned integer |
Int |
32-bit signed integer |
UInt |
32-bit unsigned integer |
Long |
64-bit signed integer |
ULong |
64-bit unsigned integer |
|
|
Floating-point types |
3 kinds |
Float |
Single-precision floating-point number |
Double |
Double-precision floating-point number |
Decimal |
High precision decimal type, valid digit precision bit 28 bits. |
Table 22 Types of non-numeric values
Type name |
Comments |
Char |
Unicode character type |
bool |
Boolean type |
②3 types of non-simple type
Type name |
Comments |
Object |
All other types of base classes |
String |
A sequence of 0 or more Unicode characters |
Dynamic |
used when assemblies written in dynamic languages are used |
There are 6 types of ③c# that can be created by the user themselves, namely class type, struct type (struct), array type (array), enum type (enum), The delegate type (delegate), the interface type (interface).
Second, stack and heap
1, Stack
① is an array of memory , a last-in, first-out (LIFO) data structure.
The ② stack stores several types of data: The value of certain types of variables , the current execution environment of the program , and the arguments passed to the method .
③ data can only be inserted and deleted from the top of the stack.
④ puts the data on top of the stack and becomes the stack (push).
⑤ Delete data from the top of the stack is called a stack (pop).
2. Heap
① is an area of memory in which large chunks of memory can be allocated to store data Objects of a certain type.
The ② heap differs from the stack in that the memory in the heap can be stored and deleted in any order .
Three, value types and reference types
types, in addition to determining the amount of memory required to store the data and the data members that make up the type, also determine where objects of the type are stored in memory, and the type is divided into two types , value types and reference types.
1, value type: data stored in the stack .
2. Reference type: The reference is stored in the stack, and the data is stored in the heap .
3. When the data is a member of another object, it will be stored in a different way.
Data for objects of the ① reference type are always stored in the heap.
A reference to the data or reference type of an object of the ② value type can be stored in the stack or stored in the heap, depending on the actual environment.
As an instance of reference type S, it has two members, value type member A, reference type member B.
Although member A is a value type, It is also part of the data for instance s of the reference type , so it is stored in the heap.
While member B is a reference type, its data part is always stored in the heap, and its reference part is also stored in the heap because it is part of the data of S .
Iv. variables
1. Classification
① local variable: Saves temporary data at the scope of the method, not a member of the type .
② field: A member of a type that holds data related to a type or type instance.
③ parameter: A temporary variable that is used to pass data from one method to another, not a member of a type .
④ array elements: a member of an ordered collection that is usually composed of homogeneous data items, either as local variables or as members of a type .
2. A local variable with no initialization statement has an undefined value that cannot be used until it is assigned, and attempting to use an undefined local variable causes the compiler to produce an error message .
3. Automatic initialization: Some types of variables are automatically set to default values if they are declared without initialization statements, and some do not.
Table three variables
Variable type |
Storage location |
Whether to initialize automatically |
Local variables |
Stacks or stacks and heaps |
Whether |
Class field |
Heap |
Is |
Structure Field |
Stack or heap |
Is |
Parameters |
Stack |
Whether |
Array type |
Heap |
Is |
4. Multi-Variable declaration
① variables in a multi-variable declaration must be of the same type .
② variable names must be separated by commas , and you can include the initialization statement after the name of the variable .
5. Static type and dynamic keyword
① static Type: The type of the variable is determined at compile time and cannot be modified at run time.
②dynamic Keyword: Represents a specific, real C # type that knows how to resolve itself at run time. At compile time, the compiler does not type check for variables of type dynamic ,
Instead, it packs all the information related to the variable and the operation of the variable. at run time, this information will be checked to ensure that it is consistent with the actual type represented by the variable .
Otherwise, an exception will be thrown at run time.
6. Nullable type
In some cases, especially when using a database, you want to indicate that the variable is not currently holding a valid value. for reference types , you can set the variable to null. But when you define a variable of a value type, regardless of whether its contents are valid,
Its memory will be allocated. In this case, you can use a Boolean indicator to associate with a variable, set to True if the value is valid, otherwise set to false, and can be resolved with a nullable type, which allows a nullable type to create a value type that can be marked as valid or invalid.
This allows you to determine whether the value is valid before using it.
[First day] C # Learning Notes