• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# Enum类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中System.Enum的典型用法代码示例。如果您正苦于以下问题:C# Enum类的具体用法?C# Enum怎么用?C# Enum使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Enum类属于System命名空间,在下文中一共展示了Enum类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;

public class EnumTest {
    enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
    enum BoilingPoints { Celsius = 100, Fahrenheit = 212 };
    [Flags]
    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);
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:31,代码来源:Enum


示例2: Main

public class Example
{
   public static void Main()
   {
      ArrivalStatus status = ArrivalStatus.OnTime;
      Console.WriteLine("Arrival Status: {0} ({0:D})", status);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:8,代码来源:Enum

输出:

Arrival Status: OnTime (0)


示例3:

int value3 = 2;
ArrivalStatus status3 = (ArrivalStatus) value3;

int value4 = (int) status3;
开发者ID:.NET开发者,项目名称:System,代码行数:4,代码来源:Enum


示例4: Main

//引入命名空间
using System;

public enum ArrivalStatus { Unknown=-3, Late=-1, OnTime=0, Early=1 };

public class Example
{
   public static void Main()
   {
      int[] values = { -3, -1, 0, 1, 5, Int32.MaxValue };
      foreach (var value in values)
      {
         ArrivalStatus status;
         if (Enum.IsDefined(typeof(ArrivalStatus), value))
            status = (ArrivalStatus) value;
         else
            status = ArrivalStatus.Unknown;
         Console.WriteLine("Converted {0:N0} to {1}", value, status);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:21,代码来源:Enum

输出:

Converted -3 to Unknown
Converted -1 to Late
Converted 0 to OnTime
Converted 1 to Early
Converted 5 to Unknown
Converted 2,147,483,647 to Unknown


示例5:

ArrivalStatus status = ArrivalStatus.Early;
var number = Convert.ChangeType(status, Enum.GetUnderlyingType(typeof(ArrivalStatus)));
Console.WriteLine("Converted {0} to {1}", status, number);
开发者ID:.NET开发者,项目名称:System,代码行数:3,代码来源:Enum

输出:

Converted Early to 1


示例6: if

string number = "-1";
string name = "Early";

try {
   ArrivalStatus status1 = (ArrivalStatus) Enum.Parse(typeof(ArrivalStatus), number);
   if (!(Enum.IsDefined(typeof(ArrivalStatus), status1)))
      status1 = ArrivalStatus.Unknown;
   Console.WriteLine("Converted '{0}' to {1}", number, status1);
}
catch (FormatException) {
   Console.WriteLine("Unable to convert '{0}' to an ArrivalStatus value.", 
                     number);
}   
   
ArrivalStatus status2;
if (Enum.TryParse<ArrivalStatus>(name, out status2)) {
   if (!(Enum.IsDefined(typeof(ArrivalStatus), status2)))
      status2 = ArrivalStatus.Unknown;
   Console.WriteLine("Converted '{0}' to {1}", name, status2);
}
else {
   Console.WriteLine("Unable to convert '{0}' to an ArrivalStatus value.", 
                     number);
}
开发者ID:.NET开发者,项目名称:System,代码行数:24,代码来源:Enum

输出:

Converted '-1' to Late
Converted 'Early' to Early


示例7:

string[] formats= { "G", "F", "D", "X"};
ArrivalStatus status = ArrivalStatus.Late;
foreach (var fmt in formats)
   Console.WriteLine(status.ToString(fmt));
开发者ID:.NET开发者,项目名称:System,代码行数:4,代码来源:Enum

输出:

Late
Late
-1
FFFFFFFF


示例8: typeof

string[] names = Enum.GetNames(typeof(ArrivalStatus));
Console.WriteLine("Members of {0}:", typeof(ArrivalStatus).Name);
Array.Sort(names);
foreach (var name in names) {
   ArrivalStatus status = (ArrivalStatus) Enum.Parse(typeof(ArrivalStatus), name);
   Console.WriteLine("   {0} ({0:D})", status);
}
开发者ID:.NET开发者,项目名称:System,代码行数:7,代码来源:Enum

输出:

Members of ArrivalStatus:
Early (1)
Late (-1)
OnTime (0)
Unknown (-3)


示例9: typeof

var values = Enum.GetValues(typeof(ArrivalStatus));
Console.WriteLine("Members of {0}:", typeof(ArrivalStatus).Name);
foreach (ArrivalStatus status in values) {
   Console.WriteLine("   {0} ({0:D})", status);
}
开发者ID:.NET开发者,项目名称:System,代码行数:5,代码来源:Enum

输出:

Members of ArrivalStatus:
OnTime (0)
Early (1)
Unknown (-3)
Late (-1)


示例10:

Pets familyPets = Pets.Dog | Pets.Cat;
if (familyPets.HasFlag(Pets.Dog))
   Console.WriteLine("The family has a dog.");
开发者ID:.NET开发者,项目名称:System,代码行数:3,代码来源:Enum

输出:

The family has a dog.


示例11:

Pets familyPets = Pets.Dog | Pets.Cat;
if ((familyPets & Pets.Dog) == Pets.Dog)
   Console.WriteLine("The family has a dog.");
开发者ID:.NET开发者,项目名称:System,代码行数:3,代码来源:Enum

输出:

The family has a dog.


示例12:

Pets familyPets = Pets.Dog | Pets.Cat;
if (familyPets == Pets.None)
   Console.WriteLine("The family has no pets.");
else
   Console.WriteLine("The family has pets.");
开发者ID:.NET开发者,项目名称:System,代码行数:5,代码来源:Enum

输出:

The family has pets.


示例13: Passing

//引入命名空间
using System;

// Define an enumeration to represent student grades.
public enum Grades { F = 0, D = 1, C = 2, B = 3, A = 4 };

// Define an extension method for the Grades enumeration.
public static class Extensions
{        
  public static Grades minPassing = Grades.D;

  public static bool Passing(this Grades grade)
  {
      return grade >= minPassing;
  }
}

class Example
{       
  static void Main()
  {
      Grades g1 = Grades.D;
      Grades g2 = Grades.F;
      Console.WriteLine("{0} {1} a passing grade.", g1, g1.Passing() ? "is" : "is not");
      Console.WriteLine("{0} {1} a passing grade.", g2, g2.Passing() ? "is" : "is not");

      Extensions.minPassing = Grades.C;
      Console.WriteLine("\nRaising the bar!\n");
      Console.WriteLine("{0} {1} a passing grade.", g1, g1.Passing() ? "is" : "is not");
      Console.WriteLine("{0} {1} a passing grade.", g2, g2.Passing() ? "is" : "is not");
  }
}
开发者ID:.NET开发者,项目名称:System,代码行数:32,代码来源:Enum

输出:

D is a passing grade.
F is not a passing grade.

Raising the bar!

D is not a passing grade.
F is not a passing grade.



注:本文中的System.Enum类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Environment类代码示例发布时间:2022-05-24
下一篇:
C# EntryPointNotFoundException类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap