Learn C # (Structure 3) a week

zhaozj2021-02-16  52

Search C # (Structure 1) C # Talent Bird (QQ: 249178521)

10. Constant field

· Constant field ...

W is implied to static

W must initialize during declaration

w Must be initialized to compile the constant value

W is only simple type, enumeration, string can be constant

Struct Pair

{

Public Pair (int x, int y)

{

//???

}

...

Private const INT x = 0, y = 0;

}

In C #, the constant field is implied to Static, but you can't explicitly declare a constant field is static:

Static const INT x = 0; // Error

Constants must be initialized and can only initialize when declaring:

Const int x; // error

Constants must be initialized to compile time:

const Int x = method (); // Error

Only simple types, enumeration, strings can be declared as constant:

Const Pair P = New Pair (); // Error

11. Find the wrong

Struct hhg

{

HHG (Int Tothegalaxy)

{

Question = 9 * 6; 1

...

}

String cover = "don't panic"; 2

Static const Int Answer = 42; 3

Const Hiker Arthur = new hiker (); 4

Const Int Question; 5

...

}

Error of the first statement: It cannot initialize constants in the constructor, and constants can only initialize the declaration.

Error of the second statement: The structure cannot be initialized when the instance field is declared, but it is possible in the class.

Error of the third statement: It is not possible to explicitly declare that the constant field is static, and the constant field can only implicit to static.

The fourth statement error: Constant fields can only be used for simple types, enumerations, strings.

Error of the 5th statement: The constant field is not initialized when the declaration is declared.

12. Local variable

· Local variables ...

w can be declared as const (Rule with the field)

w can't be declared as static or or or

Struct Pair

{

Void okay ()

{

Const int Answer = 42;

...

}

void compiletimeerrors ()

{

const Int local = call (); // error, must be initialized to compile time value

Const Pair Origin = ...; // Error, constant can only be simple type, enumeration, string

Readonly Pair P = ...; // Error, only fields can be declared as readonly

...

}

...

}

13. Static constructor function

· Static constructor initialization class

w can initialize the Static field instead of the const field

W When class is loaded, it is called by .NET call

w can't be called: no parameters, no access to the modifier

Struct Pair

{

Public Static Readonly Pair Origin; Public Pair (int x, int y)

{

THIS.X = X;

THIS.Y = Y;

}

Static Pair ()

{

Origin = new pair (0, 0);

}

Private int x, y;

}

The static constructor can only be called by .NET, not by programmers. This guarantees that it will be called, which is called once and is called in the appropriate time (before any class or structure is used). Since the programmer cannot call the static constructor, the static constructor has no parameters. For the same reason, the static constructor cannot have access to the modifier.

Static constructor cannot be used to initialize a constant field, even if the constant field is implicitly static. Because of the previous speech, the constant field must be initialized, and can only be initialized when it declares.

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

New Post(0)