关于C#中的枚举

接着上一篇讲的特性在枚举中的应用:http://blog.csdn.net/joyhen/article/details/39577565

今天着重细说一下枚举对象,本文摘录于:http://www.codeproject.com/Articles/808001/Diving-in-OOP-Day-Understanding-Enums-in-Csharp-A

(ps,codeproject上的文章还是不错的,可以经常逛逛这个坛子)

Program.cs

namespace Enums
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

    enum Color
    {
        Yellow,
        Blue,
        Brown,
        Green
    }
}
test:

Console.WriteLine(Color.Yellow);
Console.ReadLine();
Output Yellow
Now just  typecast Color.Yellow  to  int , what do we get?

Console.WriteLine((int)Color.Yellow);
Console.ReadLine();
Output 0

重点说明来了:

We see that enum is called as static variables, so an enum can be considered here as static objects. Therefore other enums in the above example can be declared in the same way as Yellow, like Blue can be declared as Color.Blue. The output in the above two examples we see is when we typecast and Yellowwithout typecasting, hence we see here that its behaviour is very similar to an array where Yellow has a value 0, similarly Blue has a value 1Brown2Green3.

Therefore, when we do Color.Yellow, it’s like displaying a number 0, so from this can we infer that an enumrepresents a constant number, therefore an enum type is a distinct type having named constants.

Point to remember : An  enum  represents for a constant number, and an  enum  type is known as a distinct type having named constants.

大致意思是说,枚举它代表者一类(下面有说明,只有部分类型可以)类型的分类表现(可以理解为派生,个人觉得表现比较好理解),它的每个对象都能知道自己所代表的值。下面是作者的例子:

using System;
namespace Enums
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine((byte)Color.Yellow);
            Console.WriteLine((byte)Color.Blue);
            Console.ReadLine();
        }
    }

    enum Color:byte
    {
        Yellow,
        Blue,
        Brown,
        Green
    }
}
输出:

0
1

这个时候Color对象的成员指定的值是byte类型,Color枚举是byte类型某一分类的表现集合,它的成员清楚的知道自己代表的是byte。默认情况下,枚举所代表的是Int型

The defaultdatatype for the enum is int

同时:

There are more data types that can be specified for enum like longulongshortushortintuintbyteand sbyte.

但是有一点需要注意,num objects data type can only be number. enum can’t be derived from any other type except that of type bytesbyteshort,ushortintuintlong, or ulong.

Point to remember: By default, enum is a sealed class and therefore sticks to all the rules that a sealed class follows, so no class can derive from enum, i.e., a sealed type.

To add more,  enum  is also derived from three interfaces  IComparable IFormattable  and  IConvertible .

using System;

namespace Enums
{
    internal enum Color
    {
        Yellow,
        Blue,
        Green
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine(Color.Yellow.CompareTo(Color.Blue));
            Console.WriteLine(Color.Blue.CompareTo(Color.Green));
            Console.WriteLine(Color.Blue.CompareTo(Color.Yellow));
            Console.WriteLine(Color.Green.CompareTo(Color.Green));
            Console.ReadLine();
        }
    }
}
输出:

-1
-1
 1
 0
//CompareTo方法说明=》 返回结果: 
//     一个有符号数,用于指示此实例和 target 的相对值。 值 含义 小于零 此实例的值小于 target 的值。 零 此实例的值等于 target
//     的值。 大于零 此实例的值大于 target 的值。 - 或 - target 为 null。
Format  is the method derived from  IFormatter  interface. It’s a  static  method so can be used directly with the  enum  class defined as  Color . It’s first parameter is the type of the  enum  class, second is the member that has to be formatted and third is the format, i.e., hexadecimal or decimal, like we used in the above example, and we got a positive result output too.
using System;

namespace Enums
{
    internal enum Color
    {
        Yellow,
        Blue,
        Green
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            System.Console.WriteLine(Color.Format(typeof(Color), Color.Green, "X"));
            System.Console.WriteLine(Color.Format(typeof(Color), Color.Green, "d"));
            Console.ReadLine();
        }
    }
}
输出:

00000002
2
using System;

namespace Enums
{
     enum Color
    {
        Yellow,
        Blue,
        Green
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            string[] names;
            names = Color.GetNames(typeof (Color));
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }
            Console.ReadLine();
        }
    }
}
Yellow
Blue
Green
using System;

namespace Enums
{
     enum Color
    {
        Yellow,
        Blue,
        Green
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
           Console.WriteLine(Color.Blue.ToString());
           Console.WriteLine(Color.Green.ToString());
           Console.ReadLine();
        }
    }
}
Blue
Green
using System;
namespace Enums
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine((int)Color.Yellow);
            Console.WriteLine((int)Color.Blue);
            Console.WriteLine((int)Color.Brown);
            Console.WriteLine((int)Color.Green);

            Console.ReadLine();
        }
    }

    enum Color
    {
        Yellow =2,
        Blue,
        Brown=9,
        Green,

    }
}
2
3
9
10
下面的例子注意和上面有什么地方不同

using System;
namespace Enums
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine((int)Color.Yellow);
            Console.WriteLine((int)Color.Blue);
            Console.WriteLine((int)Color.Brown);
            Console.WriteLine((int)Color.Green);

            Console.ReadLine();
        }
    }

    enum Color
    {
        Yellow = 2,
        Blue,
        Brown = 9,
        Green = Yellow
    }
}

2
3
9
2
我们将Green的值用Yellow来赋了,试想一下,如果Green下面还有其它项,会输出什么呢,答案是3(2[Yellow]+1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值