C# beginner, data type

Time:2022-4-4

Value type

  Data type, character type, boolean type, date type

Structure type (struct), enumeration type (enum)

reference type

  String, array, class, delegate type

 

explain:
(1) Value type data – usually allocated in the stack, and the actual data value is stored in the variable.
(2) The actual data of the reference type is allocated in the managed heap, and the reference variable is allocated in the stack; The reference type variable stores the storage address (i.e. reference) of the instance data in the heap.

 

C# data type

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Enumeration types are unique types with named constants.
The declaration of enumeration is as follows:
Access modifier enum enum name [: base type]
{
Enumerating members
}

Where: the basic type is one of 8 integer types, and the default is int. Each enumeration member is a named constant with an integer value. The default value of the first enumeration member is zero, and the subsequent member values are obtained by adding 1 to the value of the previous enumeration member.

Enumeration examples:

public enum TimeofDay
{
  Morning=1 ,
  Afternoon ,
Evening / / there is no comma at the end,
}
If the value of morning is 1, use – timeofday Morning
The value of afternoon is 2. Use – timeofday Afternoon
The value of evolving is 3, using – timeofday Evening

 

 

Data type of C # – structure
The structure type in C # is similar to that of a class. You can define a structure containing data members and method members. However, unlike classes, structures are value types and do not require heap allocation.

Instance field variables cannot be initialized in the declaration of a structure. (const, except static field).

A constructor with parameters can be declared in the structure (initializing the data members in the structure), and an instance of the structure can be created through new.

 

01:struct A
02:{
03:  public int x; // It cannot be assigned directly
04:  public int y;
05:  public static string str = null; // Static variables can be initialized
06: public a (int x, inty) / / constructor with parameters
07:  {
08:    this.x= x;
09:    this.y= y;
10:    Console.WriteLine(“x={0},y={1},str={2}”, x, y,str);
11:  }
12:}

When the parameter name in the method has the same name as the field name in the class, such as:
Lines 03 and 06, this X will reference the field name in the instance.

 

constant
Symbolic Constant – declared with const modifier.
Symbolic constants must be initialized at declaration time.
For example:
public constintmonths=12;
Declare multiple constants of the same type, for example:
constintmonths = 12, weeks = 52, days = 365;
Literal constant:

Int type constant (without decimal), e.g. 25

Double type constant, such as: 3.14, 314e-2

 

Implicit type var
C #’s local variables can be declared using the implicit type var. The format is:
Var variable name = initial value expression;
The actual type of the variable is inferred from the initial value expression.
For example: vari = 10// Implicitly declare variables, I can be inferred as int
var s=“abcd”; // Implicitly declare variables, s inferred as strings
be careful:

Variables declared by VAR must be local variables.

Must be initialized at declaration time

Example: output the value of each element of array a
for( var i=0;i<6;i++)
Console.Write(A[i] + ” “);