Enum 構造体とは? わかりやすく解説

Enum 構造体

列挙体の基本クラス提供します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class
 Enum
    Inherits ValueType
    Implements IComparable, IFormattable, IConvertible
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public abstract class Enum : ValueType, IComparable,
 IFormattable, IConvertible
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class Enum abstract : public
 ValueType, IComparable, IFormattable, IConvertible
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public abstract class Enum extends ValueType
 implements IComparable, IFormattable, 
    IConvertible
SerializableAttribute 
ComVisibleAttribute(true) 
public abstract class Enum extends
 ValueType implements IComparable, IFormattable, 
    IConvertible
解説解説

列挙体は、基になる型が Char 以外の整数型である名前付定数です。基になる型が明示的に宣言されない場合は、Int32 が使用されます。通常プラミング言語には、名前付定数とその値の組み合わせから成る列挙体を宣言する構文用意されています。

Enum には、このクラス複数インスタンス比較したり、インスタンスの値を文字列形式変換したり、数値文字列形式をこのクラスインスタンス変換したり、指定した列挙体および値のインスタンス作成するためのメソッド用意されています。

列挙体をビット フィールドとして扱うこともできます詳細については、「FlagsAttribute」を参照してください

実装されているインターフェイス

このクラスは、ValueType から継承し、IComparable、IFormattable、IConvertible の各インターフェイス実装ます。このクラス明示的な IConvertible インターフェイス メンバ実装代わりにConvert クラス変換のために使用します

FlagsAttribute および Enumガイドライン
使用例使用例

前付きの値を表す列挙体と、名前付きのビット フィールドを表す別の列挙体を使用するコード例次に示します

Imports System

Public Class EnumTest
    
    Enum Days
        Saturday
        Sunday
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
    End Enum 'Days
    
    Enum BoilingPoints
        Celcius = 100
        Fahrenheit = 212
    End Enum 'BoilingPoints
    
    <FlagsAttribute()> _
    Enum Colors
        Red = 1
        Green = 2
        Blue = 4
        Yellow = 8
    End Enum 'Colors

    Public Shared Sub Main()
        Dim weekdays As Type = GetType(Days)
        Dim boiling As Type = GetType(BoilingPoints)

        Console.WriteLine("The days of the week, and their corresponding
 values in the Days Enum are:")

        Dim s As String
        For Each s In  [Enum].GetNames(weekdays)
            Console.WriteLine("{0,-11} = {1}", s,
 [Enum].Format(weekdays, [Enum].Parse(weekdays, s), "d"))
        
        Next s
        Console.WriteLine()
        Console.WriteLine("Enums can also be created which have
 values that represent some meaningful amount.")
        Console.WriteLine("The BoilingPoints Enum defines the
 following items, and corresponding values:")

        For Each s In  [Enum].GetNames(boiling)
            Console.WriteLine("{0,-11} = {1}", s,
 [Enum].Format(boiling, [Enum].Parse(boiling, s), "d"))
        Next s

        Dim myColors As Colors = Colors.Red
 Or Colors.Blue Or Colors.Yellow
        Console.WriteLine()
        Console.WriteLine("myColors holds a combination of colors.
 Namely: {0}", myColors)
    End Sub 'Main
End Class 'EnumTest
using System;

public class EnumTest {
    enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
    enum BoilingPoints { Celcius = 100, Fahrenheit = 212 };
    [FlagsAttribute]
    enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

    public static void Main()
 {

        Type weekdays = typeof(Days);
        Type boiling = typeof(BoilingPoints);

        Console.WriteLine("The days of the week, and their corresponding values
 in the Days Enum are:");

        foreach ( string s in
 Enum.GetNames(weekdays) )
            Console.WriteLine( "{0,-11}= {1}", s, Enum.Format( weekdays,
 Enum.Parse(weekdays, s), "d"));

        Console.WriteLine();
        Console.WriteLine("Enums can also be created which have values that
 represent some meaningful amount.");
        Console.WriteLine("The BoilingPoints Enum defines the following items,
 and corresponding values:");

        foreach ( string s in
 Enum.GetNames(boiling) )
            Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling,
 Enum.Parse(boiling, s), "d"));

        Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;
        Console.WriteLine();
        Console.WriteLine("myColors holds a combination of colors. Namely: {0}"
,
 myColors);
    }
}
using namespace System;
enum class Days
{
   Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday
};

enum class BoilingPoints
{
   Celcius = 100,
   Fahrenheit = 212
};

[FlagsAttribute]

enum class Colors
{
   Red = 1,
   Green = 2,
   Blue = 4,
   Yellow = 8
};

int main()
{
   Type^ weekdays = Days::typeid;
   Type^ boiling = BoilingPoints::typeid;
   Console::WriteLine(  "The days of the week, and their corresponding values
 in the Days Enum are:" );
   Array^ a = Enum::GetNames( weekdays );
   Int32 i = 0;
   do
   {
      Object^ o = a->GetValue( i );
      Console::WriteLine(  "{0,-11}= {1}", o->ToString(), Enum::Format(
 weekdays, Enum::Parse( weekdays, o->ToString() ),  "d" ) );
   }
   while ( ++i < a->Length );

   Console::WriteLine();
   Console::WriteLine(  "Enums can also be created which have values that represent
 some meaningful amount." );
   Console::WriteLine(  "The BoilingPoints Enum defines the following items,
 and corresponding values:" );
   i = 0;
   Array^ b = Enum::GetNames( boiling );
   do
   {
      Object^ o = b->GetValue( i );
      Console::WriteLine(  "{0,-11}= {1}", o->ToString(), Enum::Format(
 boiling, Enum::Parse( boiling, o->ToString() ),  "d" ) );
   }
   while ( ++i < b->Length );

   Array^ c = Enum::GetNames( Colors::typeid );
   Colors myColors = Colors::Red | Colors::Blue | Colors::Yellow;
   Console::WriteLine();
   Console::Write(  "myColors holds a combination of colors. Namely:" );
   for ( i = 0; i < 3; i++ )
      Console::Write(  " {0}", c->GetValue( i ) );
}
import System.*;

public class EnumTest
{
    enum Days
    {
        saturday (0),
        sunday (1),
        monday (2),
        tuesday (3),
        wednesday (4),
        thursday (5),
        friday (6);
    } //Days

    enum BoilingPoints
    {
        celsius (100),
        fahrenheit (212);
    } //BoilingPoints

    /** @attribute FlagsAttribute()
     */
    enum Colors
    {
        red (1),
        green (2),
        blue (4),
        yellow (8);
    } //Colors

    public static void main(String[]
 args)
    {
        Type weekdays = Days.class.ToType();
        Type boiling = BoilingPoints.class.ToType();

        Console.WriteLine("The days of the week, and their corresponding"
            + " values in the Days Enum are:");
        String s[] = Enum.GetNames(weekdays);
        for (int iCtr = 0; iCtr < s.length;
 iCtr++) {
            Console.WriteLine("{0,-11}= {1}", s[iCtr],
                Enum.Format(weekdays, Enum.Parse(weekdays, s[iCtr]), "d"));
        }

        Console.WriteLine();
        Console.WriteLine("Enums can also be created which have values that"
            + " represent some meaningful amount.");
        Console.WriteLine("The BoilingPoints Enum defines the following items
,"
            + " and corresponding values:");
        String s1[] = Enum.GetNames(boiling);
        for (int iCtr = 0; iCtr < s1.length;
 iCtr++) {
            Console.WriteLine("{0,-11}= {1}", s1[iCtr], 
                Enum.Format(boiling, Enum.Parse(boiling, s1[iCtr]), "d"));
        }
        Colors myColors = Colors.red | Colors.blue | Colors.yellow;
        Console.WriteLine();
        Console.WriteLine("myColors holds a combination of colors. Namely: {0}"
,
            myColors);
    } //main
} //EnumTest
import System;

public class EnumTest {
    enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
    enum BoilingPoints { Celcius = 100, Fahrenheit = 212 };
    FlagsAttribute
    enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

    public static function
 Main() {

        var weekdays : Type = Days;
        var boiling : Type = BoilingPoints;

        Console.WriteLine("The days of the week, and their corresponding values
 in the Days Enum are:");

        for( var i : int
 in Enum.GetNames(weekdays) )
            Console.WriteLine( "{0,-11}= {1}", Enum.GetNames(weekdays).GetValue(i),
 
            Enum.Format( weekdays, Enum.Parse(weekdays, Enum.GetNames(weekdays).GetValue(i)),
 "d"));

        Console.WriteLine();
        Console.WriteLine("Enums can also be created which have values that
 represent some meaningful amount.");
        Console.WriteLine("The BoilingPoints Enum defines the following items,
 and corresponding values:");

        for ( var j : int
 in Enum.GetNames(boiling) )
            Console.WriteLine( "{0,-11}= {1}", Enum.GetNames(boiling).GetValue(j),
 
            Enum.Format(boiling, Enum.Parse(boiling, Enum.GetNames(boiling).GetValue(j)),
 "d"));

        var myColors : Colors = Colors.Red | Colors.Blue | Colors.Yellow;
        Console.WriteLine();
        Console.WriteLine("myColors holds a combination of colors. Namely: {0}"
,
 myColors);
    }
}
スレッド セーフスレッド セーフ

この型は、マルチスレッド操作に対して安全です。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Enum 構造体」の関連用語

Enum 構造体のお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Enum 構造体のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS