0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C# 値型(列挙型:enum)

Last updated at Posted at 2020-06-02

C#値型(列挙型)

"列挙型" は、基になる整数値型の一連の名前付き定数によって定義された値の型です。
列挙型を定義するには、enum キーワードを使用して "列挙型メンバー" の名前を指定します。

enum Season
{
	Spring,
	Summer,
	Autumn,
	Winter
}

既定では、列挙型メンバーの関連する定数値の型は int で、
0 から始まり、定義テキストの順序に従って 1 ずつ増加。
他の任意の整数値型を、列挙型の基になる型として明示的に指定も可能。

Spring Summer Autumn Winter
0 1 2 3

また、次の例に示すように、関連する定数値を明示的に指定することもできます。

enum Season
{
	Spring = 0,
	Summer = 1,
	Autumn = 100,
	Winter = 200
}
Spring Summer Autumn Winter
0 1 100 200

int型以外での宣言
:(コロン)を用いて型を指定。

enum Season:byte 
{
	Spring,
	Summer,
	Autumn = 100,
	Winter
}
Spring Summer Autumn Winter
0 1 100 101

参考資料
・Microsoft Build
・C#の絵本

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?