接着上一篇讲的特性在枚举中的应用: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 enum
s 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 0
when we typecast and Yellow
without 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 1
, Brown
: 2
, Green
: 3
.
Therefore, when we do Color.Yellow
, it’s like displaying a number 0
, so from this can we infer that an enum
represents a constant number, therefore an enum
type is a distinct type having named constants.
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 long
, ulong
, short
, ushort
, int
, uint
, byte
and sbyte
.
但是有一点需要注意,num
objects data type can only be number. enum
can’t be derived from any other type except that of type byte
, sbyte
, short
,ushort
, int
, uint
, long
, 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.
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)