Enum.Parse メソッド

定義

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。

オーバーロード

Parse(Type, ReadOnlySpan<Char>)

1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。

Parse(Type, String)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。

Parse(Type, ReadOnlySpan<Char>, Boolean)

1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

Parse(Type, String, Boolean)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

Parse<TEnum>(String, Boolean)

TEnum で指定されている 1 つ以上の列挙定数の名前または数値の文字列表現を、等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

Parse<TEnum>(ReadOnlySpan<Char>, Boolean)

TEnum 指定された 1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

Parse<TEnum>(ReadOnlySpan<Char>)

TEnum 指定された 1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。

Parse<TEnum>(String)

TEnum で指定されている 1 つ以上の列挙定数の名前または数値の文字列表現を、等価の列挙オブジェクトに変換します。

Parse(Type, ReadOnlySpan<Char>)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。

public:
 static System::Object ^ Parse(Type ^ enumType, ReadOnlySpan<char> value);
public static object Parse (Type enumType, ReadOnlySpan<char> value);
static member Parse : Type * ReadOnlySpan<char> -> obj
Public Shared Function Parse (enumType As Type, value As ReadOnlySpan(Of Char)) As Object

パラメーター

enumType
Type

列挙型。

value
ReadOnlySpan<Char>

変換する名前または値を含むスパン。

戻り値

value によって表される値を持つ enumType 型のオブジェクト。

例外

enumTypenullです。

enumTypeEnum ではありません。

value が空の文字列であるか、または空白しか含まれていません。

value は名前ですが、列挙に対して定義されている名前付き定数の名前ではありません。

valueenumType の基になる型の範囲外です。

.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。

適用対象

Parse(Type, String)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。

public:
 static System::Object ^ Parse(Type ^ enumType, System::String ^ value);
public static object Parse (Type enumType, string value);
[System.Runtime.InteropServices.ComVisible(true)]
public static object Parse (Type enumType, string value);
static member Parse : Type * string -> obj
[<System.Runtime.InteropServices.ComVisible(true)>]
static member Parse : Type * string -> obj
Public Shared Function Parse (enumType As Type, value As String) As Object

パラメーター

enumType
Type

列挙型。

value
String

変換する名前または値が含まれている文字列。

戻り値

value によって表される値を持つ enumType 型のオブジェクト。

属性

例外

enumType または valuenull です。

enumTypeEnum ではありません。

- または -

value が空の文字列であるか、または空白しか含まれていません。

- または -

value は名前ですが、列挙に対して定義されている名前付き定数の名前ではありません。

valueenumType の基になる型の範囲外です。

.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。

次の例では、Parse(Type, String)、メソッドを使用して、GetNamesメソッドを呼び出すことによって作成された文字列の配列を解析します。 また、Parse(Type, String)メソッドを使用して、ビットフィールドで構成される列挙値を解析します。

using namespace System;

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

int main()
{
   Console::WriteLine(  "The entries of the Colors enumeration are:" );
   Array^ a = Enum::GetNames( Colors::typeid );
   Int32 i = 0;
   while ( i < a->Length )
   {
      Object^ o = a->GetValue( i );
      Console::WriteLine( o->ToString() );
      i++;
   }

   Console::WriteLine();
   Object^ orange = Enum::Parse( Colors::typeid,  "Red, Yellow" );
   Console::WriteLine("The orange value has the combined entries of {0}", orange );
}

/*
This code example produces the following results:

The entries of the Colors Enum are:
Red
Green
Blue
Yellow

The orange value has the combined entries of Red, Yellow

*/
using System;

public class ParseTest
{
    [Flags]
    enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

    public static void Main()
    {
        Console.WriteLine("The entries of the Colors enumeration are:");
        foreach (string colorName in Enum.GetNames(typeof(Colors)))
        {
            Console.WriteLine("{0} = {1:D}", colorName,
                                         Enum.Parse(typeof(Colors), colorName));
        }
        Console.WriteLine();

        Colors orange = (Colors) Enum.Parse(typeof(Colors), "Red, Yellow");
        Console.WriteLine("The orange value {0:D} has the combined entries of {0}",
                           orange);
    }
}

/*
This code example produces the following results:

The entries of the Colors Enum are:
Red = 1
Green = 2
Blue = 4
Yellow = 8

The orange value 9 has the combined entries of Red, Yellow

*/
open System

[<Flags>]
type Colors =
    | Red = 1
    | Green = 2
    | Blue = 4
    | Yellow = 8

printfn "The entries of the Colors enumeration are:"
for colorName in Enum.GetNames typeof<Colors> do
    printfn $"{colorName} = {Enum.Parse(typeof<Colors>, colorName):D}"
printfn ""

let orange = Enum.Parse(typeof<Colors>, "Red, Yellow") :?> Colors
printfn $"The orange value {orange:D} has the combined entries of {orange}"

// This code example produces the following results:
//     The entries of the Colors Enum are:
//     Red = 1
//     Green = 2
//     Blue = 4
//     Yellow = 8
//    
//     The orange value 9 has the combined entries of Red, Yellow
Public Class ParseTest

    <Flags()> _
    Enum Colors
        Red = 1
        Green = 2
        Blue = 4
        Yellow = 8
    End Enum

    Public Shared Sub Main()
        Console.WriteLine("The entries of the Colors enumeration are:")
        Dim colorName As String
        For Each colorName In [Enum].GetNames(GetType(Colors))
            Console.WriteLine("{0} = {1:D}", colorName, [Enum].Parse(GetType(Colors), colorName))
        Next
        Console.WriteLine()

        Dim orange As Colors = CType([Enum].Parse(GetType(Colors), "Red, Yellow"), Colors)
        Console.WriteLine("The orange value {0:D} has the combined entries of {0}", orange)
    End Sub
End Class

'This example displays the following output:
'
'The entries of the Colors Enum are:
'Red = 1
'Green = 2
'Blue = 4
'Yellow = 8
'
'The myOrange value 9 has the combined entries of Red, Yellow
'

注釈

valueパラメーターには、列挙メンバーの基になる値または名前付き定数の文字列形式、またはコンマ (,) で区切られた名前付き定数のリストが含まれます。 valueでは、各値、名前、またはコンマの前または後に 1 つ以上の空白文字を含めることができます。 valueがリストの場合、戻り値は指定された名前の値を、ビットごとにOR演算で組み合わせた値となります。

valueenumTypeの名前付き定数に対応しない名前である場合、メソッドはArgumentExceptionをスローします。 valueenumType列挙体の基になる値を表さない整数の文字列表現の場合、メソッドは、基になる値が整数型に変換されたvalueである列挙体のメンバーを返します。 この動作が望ましくない場合は、IsDefinedメソッドを呼び出して、整数の特定の文字列形式が実際にenumTypeのメンバーであることを確認します。 次の例では、Colors列挙体を定義し、Parse(Type, String)メソッドを呼び出して文字列を対応する列挙値に変換し、IsDefinedメソッドを呼び出して、特定の整数値がColors列挙体の基になる値であることを確認します。

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       'blue' is not a member of the Colors enumeration.
//       Converted 'Blue' to Blue.
//       'Yellow' is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
open System

[<Flags>]
type Colors =
    | None = 0
    | Red = 1
    | Green = 2
    | Blue = 4

let colorStrings = [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ]
for colorString in colorStrings do
    try
        let colorValue = Enum.Parse(typeof<Colors>, colorString) :?> Colors
        if Enum.IsDefined(typeof<Colors>, colorValue) || (string colorValue).Contains "," then
            printfn $"Converted '{colorString}' to {colorValue}."
        else
            printfn $"{colorString} is not an underlying value of the Colors enumeration."
    with :? ArgumentException ->
        printfn $"'{colorString}' is not a member of the Colors enumeration."
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       'blue' is not a member of the Colors enumeration.
//       Converted 'Blue' to Blue.
//       'Yellow' is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Try
            Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString), Colors)        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Catch e As ArgumentException
            Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString)
         End Try
      Next
   End Sub
End Module
' The example displays the following output:
'       Converted '0' to None.
'       Converted '2' to Green.
'       8 is not an underlying value of the Colors enumeration.
'       'blue' is not a member of the Colors enumeration.
'       Converted 'Blue' to Blue.
'       'Yellow' is not a member of the Colors enumeration.
'       Converted 'Red, Green' to Red, Green.

この操作では大文字と小文字が区別されます。

こちらもご覧ください

適用対象

Parse(Type, ReadOnlySpan<Char>, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

public:
 static System::Object ^ Parse(Type ^ enumType, ReadOnlySpan<char> value, bool ignoreCase);
public static object Parse (Type enumType, ReadOnlySpan<char> value, bool ignoreCase);
static member Parse : Type * ReadOnlySpan<char> * bool -> obj
Public Shared Function Parse (enumType As Type, value As ReadOnlySpan(Of Char), ignoreCase As Boolean) As Object

パラメーター

enumType
Type

列挙型。

value
ReadOnlySpan<Char>

変換する名前または値を含むスパン。

ignoreCase
Boolean

大文字と小文字を区別しない場合は true。大文字と小文字を区別する場合は false

戻り値

value によって表される値を持つ enumType 型のオブジェクト。

例外

enumTypenullです。

enumTypeEnum ではありません。

value が空の文字列であるか、または空白しか含まれていません。

value は名前ですが、列挙に対して定義されている名前付き定数の名前ではありません。

value が の基になる型の範囲外である enumType

.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。

適用対象

Parse(Type, String, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

public:
 static System::Object ^ Parse(Type ^ enumType, System::String ^ value, bool ignoreCase);
public static object Parse (Type enumType, string value, bool ignoreCase);
[System.Runtime.InteropServices.ComVisible(true)]
public static object Parse (Type enumType, string value, bool ignoreCase);
static member Parse : Type * string * bool -> obj
[<System.Runtime.InteropServices.ComVisible(true)>]
static member Parse : Type * string * bool -> obj
Public Shared Function Parse (enumType As Type, value As String, ignoreCase As Boolean) As Object

パラメーター

enumType
Type

列挙型。

value
String

変換する名前または値が含まれている文字列。

ignoreCase
Boolean

大文字と小文字を区別しない場合は true。大文字と小文字を区別する場合は false

戻り値

value によって表される値を持つ enumType 型のオブジェクト。

属性

例外

enumType または valuenull です。

enumTypeEnum ではありません。

- または -

value が空の文字列 ("") であるか、または空白しか含まれていません。

- または -

value は名前ですが、列挙に対して定義されている名前付き定数の名前ではありません。

valueenumType の基になる型の範囲外です。

.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。

次の例では、Parse(Type, String, Boolean)、メソッドを使用して、GetNamesメソッドを呼び出すことによって作成された文字列の配列を解析します。 また、Parse(Type, String)メソッドを使用して、ビットフィールドで構成される列挙値を解析します。

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString, true);
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
open System

[<Flags>]
type Colors =
    | None = 0
    | Red = 1
    | Green = 2
    | Blue = 4

let colorStrings = [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ]
for colorString in colorStrings do
    try
        let colorValue = Enum.Parse(typeof<Colors>, colorString, true) :?> Colors
        if Enum.IsDefined(typeof<Colors>, colorValue) || (string colorValue).Contains "," then
            printfn $"Converted '{colorString}' to {colorValue}."
        else
            printfn $"{colorString} is not an underlying value of the Colors enumeration."
    with :? ArgumentException ->
        printfn $"{colorString} is not a member of the Colors enumeration."

// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Try
            Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString, True), Colors)        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Catch e As ArgumentException
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End Try
      Next
   End Sub
End Module
' The example displays the following output:
'       Converted '0' to None.
'       Converted '2' to Green.
'       8 is not an underlying value of the Colors enumeration.
'       Converted 'blue' to Blue.
'       Converted 'Blue' to Blue.
'       Yellow is not a member of the Colors enumeration.
'       Converted 'Red, Green' to Red, Green.

注釈

valueパラメーターには、列挙メンバーの基になる値または名前付き定数の文字列形式、またはコンマ (,) で区切られた名前付き定数のリストが含まれます。 valueでは、各値、名前、またはコンマの前または後に 1 つ以上の空白文字を含めることができます。 valueがリストの場合、戻り値は指定された名前の値を、ビットごとにOR演算で組み合わせた値となります。

valueenumTypeの名前付き定数に対応しない名前である場合、メソッドはArgumentExceptionをスローします。 valueenumType列挙体の基になる値を表さない整数の文字列表現の場合、メソッドは、基になる値が整数型に変換されたvalueである列挙体のメンバーを返します。 この動作が望ましくない場合は、IsDefinedメソッドを呼び出して、整数の特定の文字列形式が実際にenumTypeのメンバーであることを確認します。 次の例では、Colors列挙体を定義し、Parse(Type, String, Boolean)メソッドを呼び出して文字列を対応する列挙値に変換し、IsDefinedメソッドを呼び出して、特定の整数値がColors列挙体の基になる値であることを確認します。

ignoreCaseパラメーターは、この操作で大文字と小文字を区別するかどうかを指定します。

こちらもご覧ください

適用対象

Parse<TEnum>(String, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

TEnum で指定されている 1 つ以上の列挙定数の名前または数値の文字列表現を、等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(System::String ^ value, bool ignoreCase);
public static TEnum Parse<TEnum> (string value, bool ignoreCase) where TEnum : struct;
static member Parse : string * bool -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As String, ignoreCase As Boolean) As TEnum

型パラメーター

TEnum

列挙型。

パラメーター

value
String

変換する名前または値が含まれている文字列。

ignoreCase
Boolean

大文字と小文字を区別しない場合は true。大文字と小文字を区別する場合は false

戻り値

TEnum

value によって表される値を持つ TEnum 型のオブジェクト。

例外

TEnumEnum 型ではありません。

valuenullです。

value に列挙情報が含まれていません。

.NET 8 以降のバージョン: TEnum は、ブール値に基づく列挙型です。

適用対象

Parse<TEnum>(ReadOnlySpan<Char>, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

TEnum 指定された 1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(ReadOnlySpan<char> value, bool ignoreCase);
public static TEnum Parse<TEnum> (ReadOnlySpan<char> value, bool ignoreCase) where TEnum : struct;
static member Parse : ReadOnlySpan<char> * bool -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char), ignoreCase As Boolean) As TEnum

型パラメーター

TEnum

列挙型。

パラメーター

value
ReadOnlySpan<Char>

変換する名前または値を含むスパン。

ignoreCase
Boolean

大文字と小文字を区別しない場合は true。大文字と小文字を区別する場合は false

戻り値

TEnum

TEnum値が でvalue表される 型TEnumのオブジェクト。

例外

TEnumEnum 型ではありません。

value に列挙情報が含まれていません。

.NET 8 以降のバージョン: TEnum は、ブール型に基づく列挙型です。

適用対象

Parse<TEnum>(ReadOnlySpan<Char>)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

TEnum 指定された 1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(ReadOnlySpan<char> value);
public static TEnum Parse<TEnum> (ReadOnlySpan<char> value) where TEnum : struct;
static member Parse : ReadOnlySpan<char> -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char)) As TEnum

型パラメーター

TEnum

列挙型。

パラメーター

value
ReadOnlySpan<Char>

変換する名前または値を含むスパン。

戻り値

TEnum

TEnum値が でvalue表される 型TEnumのオブジェクト。

例外

TEnumEnum 型ではありません。

value に列挙情報が含まれていません。

.NET 8 以降のバージョン: TEnum は、ブール型に基づく列挙型です。

適用対象

Parse<TEnum>(String)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

TEnum で指定されている 1 つ以上の列挙定数の名前または数値の文字列表現を、等価の列挙オブジェクトに変換します。

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(System::String ^ value);
public static TEnum Parse<TEnum> (string value) where TEnum : struct;
static member Parse : string -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As String) As TEnum

型パラメーター

TEnum

列挙型。

パラメーター

value
String

変換する名前または値が含まれている文字列。

戻り値

TEnum

value によって表される値を持つ TEnum 型のオブジェクト。

例外

TEnumEnum 型ではありません。

valuenullです。

value に列挙情報が含まれていません。

.NET 8 以降のバージョン: TEnum は、ブール型に基づく列挙型です。

適用対象