C#枚举 - 特征或缺陷 [英] C# Enums - Feature or deficiency

查看:66
本文介绍了C#枚举 - 特征或缺陷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有


Enum en

{

i1,

i2,< br $> b $ b i3

}


我估计我应该可以拥有


int [] v = new int [en];


很明显我想做什么,但编译器不喜欢它。


我知道这样做的唯一方法是(有更好的方法吗?)


v = new int [Enum.GetValues(typeof(en))。GetLength(0) ];

If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn''t like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

推荐答案

好吧,看看你想做什么是如此明显,也许你想要

解释你想做什么,以便我们明显缺乏ESP的

区域,可以得到一个想法。

Ian Semmel < an **** @ rocketcomp.com.auwrote in message

news:422B1F174E5748EAAE0F2A995E0ABD2B @ DIMITY ...
Well, seeing as how what you want to do is so obvious, maybe you''d like to
explain what you want to do so that we, who are obviously deficient in the
area of ESP, can get an idea.
"Ian Semmel" <an****@rocketcomp.com.auwrote in message
news:422B1F174E5748EAAE0F2A995E0ABD2B@DIMITY...

如果我有


Enum en

{

i1,

i2,

i3

}


我估计我应该可以拥有


int [] v = new int [en];


很明显我想做什么,但编译器不喜欢它。


唯一的方法我知道这样做(有更好的方法吗?)


v = new int [Enum.GetValues(typeof(en))。GetLength(0)];

If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn''t like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];


On Sun,2007年12月9日16:57:44 +1000,Ian Semmel

< an * ***@rocketcomp.com.auwrote:
On Sun, 9 Dec 2007 16:57:44 +1000, "Ian Semmel"
<an****@rocketcomp.com.auwrote:

>我知道这样做的唯一方法是(有更好的方法吗?)

v = new int [Enum.GetValues(typeof(en))。GetLength(0)];
>The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];



可悲的是,没有。至少在一般情况下不是这样。 (虽然你可以使用

长度而不是GetLength(0),这有点短。)


在你的特殊情况下,你可以简单地使用(int )en.i3 + 1因为

枚举的默认基类型是int(Int32),值

从零开始,每增加一个值递增1。 br />

但这需要在添加枚举值时更改代码。

您可以添加额外的值,例如last = i3,并使用

而不是数组尺寸等等。但你还是要记住

来更新这个最后。枚举更改时的值。

-
http:// www.kynosarges.de




" Ian Semmel" < an **** @ rocketcomp.com.auwrote in message

news:422B1F174E5748EAAE0F2A995E0ABD2B @ DIMITY ...

"Ian Semmel" <an****@rocketcomp.com.auwrote in message
news:422B1F174E5748EAAE0F2A995E0ABD2B@DIMITY...

如果我有


Enum en

{

i1,

i2,

i3

}


我估计我应该可以拥有


int [] v = new int [en];


很明显我想做什么,但编译器不喜欢它。


唯一的方法我知道这样做(有更好的方法吗?)


v = new int [Enum.GetValues(typeof(en))。GetLength(0)];
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn''t like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];



我会这样做(但我不是最纯粹的):


Enum en

{

i1,

i2,

i3,


enumLength

}


int [] v = new int [enumLength];

I would do this (but I''m not a purest):

Enum en
{
i1,
i2,
i3,

enumLength
}

int[] v = new int[enumLength];


这篇关于C#枚举 - 特征或缺陷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆