C# beginners, data types

Value type

Data type, character type, boolean type, date type

Structure type (struct), enumeration type (enum)

Reference type

String (string), array, class, delegate type

Explanation:
(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 heap (managed heap), and the reference variable is allocated in the stack; the reference type variable stores the storage address (ie reference) of the instance data in the heap.

C# data type

Insert picture description here

Insert picture description here

Enumerated types are unique types with named constants.
The enumeration declaration is as follows:
access modifier enum enumeration name [: basic type]
{   enumeration member }

Among them: the basic type is one of eight integer types, and the default is int. Each enumeration member is a named constant and has an integer value. The first enum member default python based tutorial value is zero, the value of the members of the future is to get added value from the previous enum member 1.

Enumeration example:

public enum TimeofDay
{
    
    
  Morning=1 ,
  Afternoon ,
  Evening    //结束没有逗号,
}
Morning的值为1,使用-TimeofDay. Morning
Afternoon的值为2,使用-TimeofDay. Afternoon
Evening的值为3,使用-TimeofDay. Evening

C# Data Type-Structure
The structure type in C# is similar to a class, and you can define a structure that includes data members and method members. But unlike the class, the structure is a value type and does not require heap allocation.

In the declaration of the structure, the instance field variables cannot be initialized. (Except for const, static fields).
• The
structure can declare a constructor with parameters (initialize the data members in the structure), and create an instance of the structure through new.

01struct A
02{
    
    
03:  public int x; //不能直接对其进行赋值
04:  public int y;
05:  public static string str = null; //静态变量可以初始化
06:  public A(int x,inty) //带参数的构造函数
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:
Line 03 and 06, then this.x in the method will refer to the field name in the instance c# tutorial .

Constant
Symbolic constant-use the const modifier to declare.
Symbolic constants must be initialized when they are declared.
For example:
public constintmonths=12;
Declare multiple constants of the same type, for example:
constintmonths = 12, weeks = 52, days = 365;
Literal constants:

int constants (without decimals) such as: 25

double constants such as: 3.14, 314e-2

Implicit type-var
C# local variables can be declared with implicit type var. The format is:
var variable name = initial value expression;
the actual type of the variable is from the initial value table vb.net tutorial

Express inference.
Such as: vari = 10; // implicitly declare variables, i can be inferred as int
var s="abcd"; // implicitly declare variables, s is inferred as string
Note:

The variable declared by var must be a local variable.

Must be initialized at declaration

Example: output the value of each element of A array

for( var i=0;i<6;i++)
Console.Write(A[i] + " ");

Guess you like

Origin blog.csdn.net/chinaherolts2008/article/details/112853283