跟小静读CLR via C#(18)——Enum

简介:

1. Enum定义

枚举类型是经常用的一种“名称/值”的形式,例如:

public enum FeedbackStatus
     {
         New,
         Processing,
         Verify,
         Closed
     }

定义枚举类型之后我们在使用时方便了许多,不用再记着0代表什么状态,1代表什么状态。而且枚举类型时强类型的,在编译时就可以进行类型安全检查。枚举类型是值类型的,它是直接从System.Enum继承的,System.Enum又是继承自System.ValueType。但是枚举类型不可以定义方法、属性或者事件。

2. 常用方法

①Enum.GetUnderlyingType:获取枚举类型实例值的基类。

   Console.WriteLine(Enum.GetUnderlyingType(typeof(FeedbackStatus)));//结果System.Int32

ToString() :转换为字符串形式

    FeedbackStatus status=FeedbackStatus .New ;
    Console.WriteLine(status.ToString());    //结果New
    Console.WriteLine(status.ToString("G")); //结果New
    Console.WriteLine(status.ToString("D")); //结果0

 

GetValues:获取枚举类型中定义的所有符号以及对应的值。

FeedbackStatus[] status = (FeedbackStatus[])Enum.GetValues(typeof(FeedbackStatus));
            foreach(FeedbackStatus s in status )
            {
                Console.WriteLine("{0:D}--{0:G}", s);
            }

image

GetNames:获取枚举类型中定义的所有符号。

string[] arr= Enum.GetNames(typeof(FeedbackStatus));
          foreach (string name in arr)
          {
              Console.WriteLine(name);
          }

image

Parse, TryParse:将文本类型转换为对应的枚举类型。

FeedbackStatus status = (FeedbackStatus)Enum.Parse(typeof(FeedbackStatus), "New", false);
Enum.TryParse("aaa", false, out status);

IsDefine:判断一个值对于一个枚举类型是否合法。

Console .WriteLine(Enum.IsDefined(typeof(FeedbackStatus),1));    //true
Console.WriteLine(Enum.IsDefined(typeof(FeedbackStatus), "New"));//true
Console.WriteLine(Enum.IsDefined(typeof(FeedbackStatus), "new"));//false,区分大小写
Console.WriteLine(Enum.IsDefined(typeof(FeedbackStatus), "aaa"));//false
Console .WriteLine(Enum.IsDefined(typeof(FeedbackStatus ),5));   //false

 

3. 扩展方法与枚举

上面提到过枚举中是不允许定义方法和事件的。但是我们可以通过扩展方法变相的为枚举添加方法。

public  static class EnumMethod
{
    public static void Show(this FeedbackStatus status)
    {
        string[] arr = Enum.GetNames(typeof(FeedbackStatus));
        Console.WriteLine("枚举类型列表:");
        foreach (string name in arr)
        {
            Console.WriteLine(name);
        }
    }
}

static void Main(string[] args)
      {
          FeedbackStatus status = FeedbackStatus.Processing;
          status.Show();

      }

image







    本文转自 陈敬(Cathy) 博客园博客,原文链接:
http://www.cnblogs.com/janes/archive/2012/02/06/2340112.html
,如需转载请自行联系原作者
相关文章
|
存储 开发框架 Java
【CLR C#】浅谈.Net的GC(垃圾回收)机制及其整体流程
在.NET程序开发中,为了将开发人员从繁琐的内存管理中解脱出来,将更多的精力花费在业务逻辑上,CLR提供了自动执行垃圾回收的机制来进行内存管理,开发人员甚至感觉不到这一过程的存在。.NET程序可以找出某个时间点上哪些已分配的内存空间没有被程序使用,并自动释放它们。自动找出并释放不再使用的内存空间机制,就称为垃圾回收机制。本文主要介绍.Net中的GC(垃圾回收)机制及其整体流程。
【CLR C#】浅谈.Net的GC(垃圾回收)机制及其整体流程
|
存储 C# C++
C# 结构体(Struct)、 枚举(Enum)
C# 结构体(Struct) 在 C# 中,结构体是值类型数据结构。它使得一个单一变量可以存储各种数据类型的相关数据。struct 关键字用于创建结构体。 结构体是用来代表一个记录。假设您想跟踪图书馆中书的动态。您可能想跟踪每本书的以下属性:
186 0
|
存储 C# vr&ar
【100个 Unity小知识点】 | C#中通过 数字int值 获取 枚举Enum 中的数值
Unity 小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 。 包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助 Unity 将创意变成现实。 Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。 也可以简单把 Unity 理解为一个游戏引擎,可以用来专业制作游戏!
|
SQL 存储 .NET
SQL Server CLR 使用 C# 自定义存储过程和触发器
原文:SQL Server CLR 使用 C# 自定义存储过程和触发器 这一篇博客接着上一篇博客继续介绍 SQL CLR Stored Procedure 和 CLR Trigger, 上一篇博客介绍了 SQL CLR Function 的使用,以及 CLR 程序集的注册和 CLR Function 的注册。
930 0