enum烦恼... [英] enum annoyance...

查看:53
本文介绍了enum烦恼...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到C#是一种强类型语言,但作为转换为C#的C ++程序员,我发现要求我将整数枚举值强制转换为特别烦人的要求。 C#社区中的其他人是否认为编译器应该放松一下


//例如,定义一个从int类型派生的枚举


public enum水果:在


Apple = 1

Banana = 2

Cherry = 3

日期= 4

//等..

//稍后在我的代码中我想使用水果枚举来索引某些数组


股票[Fruit.Apple] + =(收到[Fruit.Apple] - 发货[Fruit.Apple])


//我已经告诉编译器了我的枚举声明水果是一个整数,所以为什么

//我必须这样做


stock [(int)Fruit.Apple ] + =(收到[(int)Fruit.Apple] - 发货[(int)Fruit.Apple])


//对我来说似乎有很多不必要的打字.. 。


很抱歉抱怨,但这是我可以发布的地方,可以实际做某事的人可能会看到它: -


- 史蒂夫

I realize that C# is a strongly typed language, but as a C++ programmer converting to C# I find the requirement that I typecast my integer enum values to be particularly annoying. Does anyone else in the C# community think that the compiler should relax this a bit

// example, define an enum that derives from an int type

public enum Fruit : in

Apple = 1
Banana = 2
Cherry = 3
Date = 4
// etc..
// later in my code I want to use the fruit enum to index some arrays

stock[ Fruit.Apple ] += ( received[ Fruit.Apple ] - shipped[ Fruit.Apple ] )

// I have already told the compiler in my enum declaration that a Fruit is an integer, so why
// do I have to do it this way

stock[ (int) Fruit.Apple ] += ( received[ (int) Fruit.Apple ] - shipped[ (int) Fruit.Apple ] )

// just seems like a lot of unnecessary typing to me...

Sorry to gripe, but this is one place I can post it where somebody who can actually do something about it might see it :-

-- Steve

推荐答案

sloughin< an ******* @ discussion.microsoft.com>写道:
sloughin <an*******@discussions.microsoft.com> wrote:
我意识到C#是一种强类型语言,但作为一名C ++程序员转换为C#,我发现要求我将我的整数枚举值强制转换为特别的烦人。 C#社区中的其他人是否认为编译器应该放松一下?


就个人而言,没有。

//例如,定义一个从int类型派生的枚举:

public enum Fruit: int
{= Apple = 1,
香蕉= 2,
樱桃= 3,
日期= 4,
//等...
//

//在我的代码中稍后我想使用水果枚举来索引一些数组:

stock [Fruit.Apple] + =(收到[Fruit。 Apple] - 发货[Fruit.Apple]);

//我已经在enum声明中告诉编译器
// Fruit是一个整数,为什么我必须这样做:


水果*不是*整数 - 它是水果。只是你告诉

编译器中Fruit和int之间的关系,即

,任何Fruit都可以表示为int。

stock [(int)Fruit.Apple] + =(收到[(int)Fruit.Apple] -
发货[(int)Fruit.Apple]);
< // @对我来说似乎有很多不必要的打字......

很抱歉抱怨,但这是我可以发布的地方,有人可以做某事
关于它可能会看到它: - )
I realize that C# is a strongly typed language, but as a C++
programmer converting to C# I find the requirement that I typecast my
integer enum values to be particularly annoying. Does anyone else in
the C# community think that the compiler should relax this a bit?
Personally, no.
// example, define an enum that derives from an int type:

public enum Fruit : int
{
Apple = 1,
Banana = 2,
Cherry = 3,
Date = 4,
// etc...
}

// later in my code I want to use the fruit enum to index some arrays:

stock[ Fruit.Apple ] += ( received[ Fruit.Apple ] - shipped[ Fruit.Apple ] );

// I have already told the compiler in my enum declaration that a
// Fruit is an integer, so why do I have to do it this way:
A Fruit *isn''t* an integer - it''s a Fruit. It''s just that you''ve told
the compiler there''s a relationship between Fruits and ints, namely
that any Fruit can be represented as an int.
stock[ (int) Fruit.Apple ] += ( received[ (int) Fruit.Apple ] -
shipped[ (int) Fruit.Apple ] );

// just seems like a lot of unnecessary typing to me...

Sorry to gripe, but this is one place I can post it where somebody
who can actually do something about it might see it :-)




我希望没有人会对它做任何事情:)


你总是可以编写一个FruitCollection,它使用Fruit作为

索引器 - 但Fruit *不是*任何数组索引器的参数,而且我

don'我不想意外地使用Fruit,我的意思是int。


-

Jon Skeet - < sk *** @ pobox.com> ;
http://www.p obox.com/~skeet

如果回复小组,请不要给我发邮件



And where I for one hope no-one will do anything about it :)

You could always write a FruitCollection which used Fruit as the
indexer - but Fruit *isn''t* the parameter for any array indexer, and I
don''t want to accidentally use a Fruit where I really mean an int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


sloughin< an * ******@discussions.microsoft.com>写道:
sloughin <an*******@discussions.microsoft.com> wrote:
我意识到C#是一种强类型语言,但作为一名C ++程序员转换为C#,我发现要求我将我的整数枚举值强制转换为特别的烦人。 C#社区中的其他人是否认为编译器应该放松一下?


就个人而言,没有。

//例如,定义一个从int类型派生的枚举:

public enum Fruit: int
{= Apple = 1,
香蕉= 2,
樱桃= 3,
日期= 4,
//等...
//

//在我的代码中稍后我想使用水果枚举来索引一些数组:

stock [Fruit.Apple] + =(收到[Fruit。 Apple] - 发货[Fruit.Apple]);

//我已经在enum声明中告诉编译器
// Fruit是一个整数,为什么我必须这样做:


水果*不是*整数 - 它是水果。只是你告诉

编译器中Fruit和int之间的关系,即

,任何Fruit都可以表示为int。

stock [(int)Fruit.Apple] + =(收到[(int)Fruit.Apple] -
发货[(int)Fruit.Apple]);
< // @对我来说似乎有很多不必要的打字......

很抱歉抱怨,但这是我可以发布的地方,有人可以做某事
关于它可能会看到它: - )
I realize that C# is a strongly typed language, but as a C++
programmer converting to C# I find the requirement that I typecast my
integer enum values to be particularly annoying. Does anyone else in
the C# community think that the compiler should relax this a bit?
Personally, no.
// example, define an enum that derives from an int type:

public enum Fruit : int
{
Apple = 1,
Banana = 2,
Cherry = 3,
Date = 4,
// etc...
}

// later in my code I want to use the fruit enum to index some arrays:

stock[ Fruit.Apple ] += ( received[ Fruit.Apple ] - shipped[ Fruit.Apple ] );

// I have already told the compiler in my enum declaration that a
// Fruit is an integer, so why do I have to do it this way:
A Fruit *isn''t* an integer - it''s a Fruit. It''s just that you''ve told
the compiler there''s a relationship between Fruits and ints, namely
that any Fruit can be represented as an int.
stock[ (int) Fruit.Apple ] += ( received[ (int) Fruit.Apple ] -
shipped[ (int) Fruit.Apple ] );

// just seems like a lot of unnecessary typing to me...

Sorry to gripe, but this is one place I can post it where somebody
who can actually do something about it might see it :-)




我希望没有人会对它做任何事情:)


你总是可以编写一个FruitCollection,它使用Fruit作为

索引器 - 但Fruit *不是*任何数组索引器的参数,而且我

don'我不想意外地使用Fruit,我的意思是int。


-

Jon Skeet - < sk *** @ pobox.com> ;
http://www.p obox.com/~skeet

如果回复小组,请不要给我发邮件



And where I for one hope no-one will do anything about it :)

You could always write a FruitCollection which used Fruit as the
indexer - but Fruit *isn''t* the parameter for any array indexer, and I
don''t want to accidentally use a Fruit where I really mean an int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


水果是水果。


它可能表示为一个整数,但它是一个水果。


我不认为这条规则应该放宽。演员非常有帮助,它表示你正在做的事情取决于你的Fruit枚举的内部

表示。所有其余的(不需要

铸造的东西)是安全的。关于你的枚举的

表示没有任何特殊的东西。


布鲁诺


" sloughin" ; <一个******* @ discussions.microsoft.com> écritdansle message de

news:01 ********************************* * @ microsof t.com ...
a Fruit is a Fruit.

It may be represented as an integer but it is a Fruit.

I don''t think that this rule should be relaxed. The cast is very helpful, it
shows that you are doing something that depends on the internal
representation of your Fruit enum. All the rest (stuff that does not need
casting) is "safe" stuff that does not assume anything special about the
representation of your enum.

Bruno

"sloughin" <an*******@discussions.microsoft.com> a écrit dans le message de
news:01**********************************@microsof t.com...
我意识到C#是一种强类型语言,但作为C ++程序员
转换为C#我发现要求我对整数枚举进行类型转换

值特别烦人。 C#社区中的其他任何人吗?b $ b认为编译器应该放松一点吗?
//例如,定义一个从int类型派生的枚举:
public enum Fruit:int
{
Apple = 1,
Banana = 2,
Cherry = 3,
Date = 4,
//等... //

//稍后在我的代码中我想使用水果枚举来索引某些数组:

stock [Fruit.Apple] + = (收到[Fruit.Apple] - 发货[
Fruit.Apple]);
//我已经在enum声明中告诉编译器,Fruit是
整数,所以为什么/ /我必须这样做:

stock [(int)Fruit.Apple] + =(收到[(int)Fruit.Apple] -
发货[(int)Fruit .Apple]);
//对我来说似乎有很多不必要的打字......

很抱歉抱怨,但这是一个我可以发布的地方,有人可以
实际上可以做一些事情可能会看到它:-)
- Steve
I realize that C# is a strongly typed language, but as a C++ programmer converting to C# I find the requirement that I typecast my integer enum
values to be particularly annoying. Does anyone else in the C# community
think that the compiler should relax this a bit?
// example, define an enum that derives from an int type:

public enum Fruit : int
{
Apple = 1,
Banana = 2,
Cherry = 3,
Date = 4,
// etc...
}

// later in my code I want to use the fruit enum to index some arrays:

stock[ Fruit.Apple ] += ( received[ Fruit.Apple ] - shipped[ Fruit.Apple ] );
// I have already told the compiler in my enum declaration that a Fruit is an integer, so why // do I have to do it this way:

stock[ (int) Fruit.Apple ] += ( received[ (int) Fruit.Apple ] - shipped[ (int) Fruit.Apple ] );
// just seems like a lot of unnecessary typing to me...

Sorry to gripe, but this is one place I can post it where somebody who can actually do something about it might see it :-)
-- Steve



这篇关于enum烦恼...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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