Enums
Forms that compile (7)
Section titled “Forms that compile (7)”| Syntax | Description |
|---|---|
public enum Mode { Off, On } | Declaring an enum |
enum Code { Ok = 200, NotFound = 404 } | Explicit values |
enum Small : byte { A = 1 } | Specifying the underlying type |
[System.Flags] enum Perm { Read = 1, Write = 2 } | Combining (Flags) |
public Mode M; | Holding as a field |
Mode[] ms = new Mode[2]; | Array element |
struct Holder { enum Mode { … } } | Declaring inside a type |
Forms that don’t compile (2)
Section titled “Forms that don’t compile (2)”| Syntax | Description | Error | Reason | Instead |
|---|---|---|---|---|
m.ToString() | Turning a value’s name into a string | TUKI0001 | undecided | Write a method that returns the name with switch |
$"{m}" | Embedding a value’s name into a string | TUKI0001 | undecided | If a number is enough, embed (int)m. If you need the name, write a method that returns the name with switch. |
| An enum value is carried as an integer. The value itself works normally, but it doesn’t have a name at runtime, so forms that retrieve the name as a string don’t compile. |
[System.Flags] only affects how the name is printed, so it doesn’t change the output in this environment. Combining with | compiles without this attribute.
Declaration
Section titled “Declaration”Declaring an enum
Section titled “Declaring an enum”using UnityEngine;using Tsukimi;
public enum Mode { Off, On }public class TyEnumDeclare : TsukimiBehaviour{ void Start() { Mode m = Mode.On; Debug.Log((int)m); // => 1 }}Explicit values
Section titled “Explicit values”using UnityEngine;using Tsukimi;
public enum Code { Ok = 200, NotFound = 404 }public class TyEnumExplicit : TsukimiBehaviour{ void Start() { Code c = Code.NotFound; Debug.Log((int)c); // => 404 }}Specifying the underlying type
Section titled “Specifying the underlying type”using UnityEngine;using Tsukimi;
public enum Small : byte { A = 1 }public class TyEnumUnderlying : TsukimiBehaviour{ void Start() { Small s = Small.A; Debug.Log((byte)s); // => 1 }}Combining (Flags)
Section titled “Combining (Flags)”Adding [System.Flags] doesn’t change how | combines values (Forms that don’t compile).
using UnityEngine;using Tsukimi;
[System.Flags]public enum Perm { None = 0, Read = 1, Write = 2 }public class TyEnumFlags : TsukimiBehaviour{ void Start() { Perm p = Perm.Read | Perm.Write; Debug.Log((int)p); // => 3 }}Storage
Section titled “Storage”Holding as a field
Section titled “Holding as a field”using UnityEngine;using Tsukimi;
public enum Mode { Off, On }public class TyEnumField : TsukimiBehaviour{ void Start() { M = Mode.On; Debug.Log((int)M); // => 1 }
public Mode M;}Array element
Section titled “Array element”using UnityEngine;using Tsukimi;
public enum Mode { Off, On }public class TyEnumArray : TsukimiBehaviour{ void Start() { Mode[] ms = new Mode[2]; ms[0] = Mode.On; Debug.Log((int)ms[0]); // => 1 }}Declaring inside a type
Section titled “Declaring inside a type”using UnityEngine;using Tsukimi;
public struct Holder { public enum Mode { Off, On } }public class TyEnumNested : TsukimiBehaviour{ void Start() { Holder.Mode m = Holder.Mode.On; Debug.Log((int)m); // => 1 }}