Language

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>)

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
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>

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

返品

enumTypeで表される値を持つvalue型のオブジェクト。

例外

enumType は nullです。

enumType は Enumではありません。

value は空の文字列か、空白のみを含みます。

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

value は、基になる型の enumTypeの範囲外です。

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

適用対象

Parse(Type, String)

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
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

変換する名前または値を含む文字列。

返品

enumTypeで表される値を持つvalue型のオブジェクト。

属性

例外

enumType または value が null。

enumType は Enumではありません。

-又は-

value は空の文字列か、空白のみを含みます。

-又は-

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

value は、基になる型の enumTypeの範囲外です。

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

例

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

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演算と組み合わせた指定された名前の値です。

valueがenumTypeの名前付き定数に対応しない名前の場合、メソッドはArgumentExceptionをスローします。 valueがenumType列挙体の基になる値を表さない整数の文字列表現である場合、メソッドは、基になる値が整数型に変換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)

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
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 。

返品

enumTypeで表される値を持つvalue型のオブジェクト。

例外

enumType は nullです。

enumType は Enumではありません。

value は空の文字列か、空白のみを含みます。

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

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

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

適用対象

Parse(Type, String, Boolean)

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
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 。

返品

enumTypeで表される値を持つvalue型のオブジェクト。

属性

例外

enumType または value が null。

enumType は Enumではありません。

-又は-

value は空の文字列 ("") であるか、空白のみを含みます。

-又は-

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

value は、基になる型の enumTypeの範囲外です。

.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演算と組み合わせた指定された名前の値です。

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

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

こちらもご覧ください

適用対象

Parse<TEnum>(String, Boolean)

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
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

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

例外

TEnum は Enum 型ではありません。

value は nullです。

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

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

適用対象

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

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
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型のオブジェクト。

例外

TEnum は Enum 型ではありません。

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

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

適用対象

Parse<TEnum>(ReadOnlySpan<Char>)

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
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型のオブジェクト。

例外

TEnum は Enum 型ではありません。

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

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

適用対象

Parse<TEnum>(String)

ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
Enum.cs
ソース:
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

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

例外

TEnum は Enum 型ではありません。

value は nullです。

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

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

適用対象