Search C # (Structure 1) a week

zhaozj2021-02-16  51

Search C # (Structure 1) a week

C # Talent Bird (QQ: 249178521)

1. Structure declaration

· Structure is a user-defined value type

Struct Pair

{

Public int x, y; // public variable name of the first letter capital (PASCALCASE rules)

}

Struct Pair

{

Private int x, y; // Non-public variable name first word first letter lowercase (Camelcase rules)

}

Struct Pair

{

INT X, Y; / / The default access modifier is private

}; / / Can have end sections

The structure is the most common mechanism for C # programmers to define their own value type. Structure is more powerful than enumeration because it provides functions, fields, constructor, operators, and access control. The default access to the structural member is Private (which is public) in C ). When you define a member name of the structure, don't forget to use the PasCalCase rules for public members, and use the Camelcase rules for non-public members.

Although the end of the structural statement can be used, it is recommended that you don't use it, this is just to take care of the C programmer's habits.

2. The result of the value

· A structural variable exists in the stack (STACK)

W field is not pre-assigned

W field can only be read after being assigned

w Use point operators to access members

The following example assumes that the Pair is a structure, it has two public integer members x, y

Static void

Main

()

{

PAIR P;

Console.write (p.x); // Error

...

}

Static void

Main

()

{

PAIR P;

P.x = 0;

Console.write (p.x); // correct

...

}

The structural variables are present in the stack. In the above example, although a PAIR structural variable called P is declared, it is actually a short written form of two local variables P.x and P.y.

The console.write of the first segment of the above example tries to use the value of P.X, but it is wrong because P.x is not assigned.

3. Initialization of the value

· A structural variable:

W Always use the default constructor to initialize

w default constructor initializes the field to 0 / false / NULL

Static void

Main

()

{

PAIR P;

Console.write (p.x); // error, P.X is not initialized

...

}

Static void

Main

()

{

PAIR P = New Pair ();

Console.write (p.x); // correct, p.x = 0

...

}

In addition to the initialization methods described above, a default constructor can be used to initialize a structural variable. The call constructor always uses the New keyword. A structural variable is a value type. It exists directly in the stack, and the use of the New keyword will not open memory in the stack. The default constructor of the structure always initializes all fields in the structural variable (you can't change this line, "will be said in the following section).

If you have a C or Java background, you may make it difficult to believe that using the New keyword to call the constructor to allocate memory in the stack, but in C # is like this. The structural variable exists in the stack, call the constructor to initialize its field, and there is no stack of memory allocations.

C programmer Note: Calling the default constructor in C # must use parentheses.

Pair P = new pair; // error

Pair P = new pair (); // correct

4. Value constructor · General rules

w compiler declares the default constructor

w You can't declare the default constructor

w Default constructor initializes all instance fields to 0 / false / NULL

Struct Pair

{

}

// Compiler declares a default constructor

Struct Pair

{

Public Pair ()

{...}

}

// Error, you can't declare the default constructor

Struct Pair

{

Public Pair (int x, int y)

{...}

}

/ / Correct, but the default constructor of the compiler declared still exists

The structural class has a public default constructor with a compiler declared. No matter if you have a declaration function, the compiler declared the public default constructor always exists. So you can't define the default constructor, which will have two default constructor, which is not allowed. However, it should be noted that this is only suitable for the structure, which is not applicable to the class. The default constructor generated by the compiler returns all instance fields:

l BOOL type is false

l Integer (including character type) is 0

l Turning into 0.0

l Enumeration is 0

l Reference type (including string) is null

The default access to the user-defined structural class constructor is the private, and the structure of the structure class.

C # does not allow you to declare a function as the constructor name.

5.:this (...)

· A constructor can call another constructor

Struct ColouredPoint

{

Public colouredpoint (int X, int y)

: this (x, y, color.red)

{

}

Public ColouredPoint (int X, int y, color c)

{

...

}

...

Private int x, y;

Private colour C;

}

转载请注明原文地址:https://www.9cbs.com/read-22157.html

New Post(0)