Search C # (enumerated one) a week

zhaozj2021-02-16  49

Search C # (enumerated one) a week

C # Talent Bird (QQ: 249178521)

Type

· Value type

W variables directly contain their own data

W Local variables are always placed in the stack (STACK)

· Quote type

W variable indirectly points to their data

w Local variables pointing to objects in Heap (HEAP)

Enumeration (ENUM) value type

Structure value type

Class (Class) reference type

Interface reference type

Array ([] array) reference type

Delegate reference type

You may be strange to the above example, the intrinsic classes in C #, such as int, how did Double? C # specifies that these intrinsic classes belong to the structure, and C # is called simple type. The maximum difference between the simple type and the user-defined type is that the former has literal expressions (such as 42), and the latter is not.

Of course, there are third types: pointers. But the pointer is only used in a non-secure code identified by the Unsafe keyword.

2. Enumeration Type

· It is a value type of a user declared

ENUM SUIT

{

Clubs, Diamonds, Hearts, Spades

}

// Suit means a pair, it has 4 colors: plumbs, squares (Diamonds), red hearts, // 黑 (spades)

Sealed Class Example

{

Static void

Main

()

{

...

Suit lead = spades; // error

...

Suit trumps = suit.clubs; // correct

...

}

}

The statement of enumerations can appear in the same place as class declaration.

Enumeration statements include names, access rights, members of the inner type and enumeration.

The scope of the constant declaration in the enumeration is to define their enumeration, in other words, the following example is incorrect:

Suit trumps = clubs;

Clubs must be restricted to a member of Suit, as follows:

Suit trumps = suit.clubs;

3. Guidelines for enumeration

· Enumeration value defaults to int

w You can choose any of the intrinsic integer types

W but can't be a character type

ENUM Suit: int // Intrinsic type is int, can be omitted

{

CLUBS,

Diamonds,

Hearts = 42, // The value of the member is default to the previous member 1, but you can assign initial values

Spades, // The last semicolon is optional

}; / / Can have end sections

Enumeration class can explicitly declare its intrinsic type is Sbyte, Byte, Short, USHORT, INT, UINT, LONG, ULONG. If an enumeration class does not explicitly declares its intrinsic type, the default is int.

The value of the member must be the same as the inner type of the enumeration statement, and must be within the range of the type (for example, you can't let the member value is negative, and the intrinsic type of the enumeration is uint).

If the member is not assigned, then its value is the previous member of the value 1, the default value of the first member is 1. The value of the enumerated member can have the same value.

The last enumeration member can use an end semicolon, which makes you easily add more members in the future.

Enumeration members' access is implicit to public.

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

New Post(0)