简体   繁体   中英

Define new value-type for struct

I was wondering if it was possible for one to make a struct or similar taking a value like a bool's true and false, but for example fluid, solid or gas. (Not a string-variable) Thanks in advance!

I think that you need is just this:

enum State { Fluid, Solid, Gas };

An enumeration of the states you have. So when you want to refer to the Fluid state, you just write this State.Fluid .

Basically, enum is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

For further documentation about enum , please have a look here .

Enums are simple value types that map names to integral values or flags.

enum State
{
    Fluid,
    Solid,
    Gas
}

Internally, State.Fluid is just 0, State.Solid is 1 and State.Gas is 2. No strings are used during runtime, unless you use ToString .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM