简体   繁体   English

为结构定义新的值类型

[英]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 . 因此,当您要引用流体状态时,只需编写此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 . 有关enum更多文档,请在此处查看

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 . 在内部, State.Fluid仅为0, State.Solid为1, State.Gas为2。除非您使用ToString ,否则在运行时不使用任何字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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