C# 列舉 IsDefined 方法


IsDefined 方法返回 true,如果給定的整數或其名稱作為字串在指定的列舉中存在。

以下為我們的列舉 −

enum Subjects { Maths, Science, English, Economics };

上述內容預設初始化為 i.e.

Maths = 0, Science = 1, English = 2, Economics = 3

因此,當我們使用 IsDefined() 查詢 3 時,它將返回 True,如下所示 −

示例

 即時演示

using System;
public class Demo {
   enum Subjects { Maths, Science, English, Economics };
   public static void Main() {
      object ob;
      ob = 3;
      Console.WriteLine("{0} = {1}", ob, Enum.IsDefined(typeof(Subjects), ob));
   }
}

輸出

3 = True

更新於: 23-Jun-2020

2K+ 瀏覽

開啟你的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.