本文介绍了将枚举作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想我之前见过有人在代码中传递了一个emumeration。


有谁能告诉我这是可能的,也是我想要的原因。

非常感谢


最基本的问候


西蒙

Hi all,

I think I''ve seen someone passing an emumeration in code before.

Can anyone tell me if thats possible and why i would want to.

Many thanks

Kindest Regards

Simon

推荐答案






不幸的是,它无法通过这样一种方法的枚举。

因为它是从
System.Enum派生的特殊(用户定义)值类型,所以需要将基础类型传递给功能。


例如,要使用enum'的成员,你必须通过

他们分别指定他们的基础类型:


私有枚举MyEnum:int {ONE = 1,TWO = 2,THREE = 3}


private void DoIt(int i)

{

开关(i)

{

案例1:

Console.WriteLine( MyEnum:{0},i);

休息;

案例2:

Console.WriteLine(" MyEnum: {0}",i);

休息;

案例3:

Console.WriteLine(" MyEnum:{0}" ;,i);

休息;

默认:

休息;

}

}


this.DoIt((int)MyEnum.ONE);

this.DoIt((int)MyEnum.TWO);

this.DoIt((int)MyEnum.THREE);


这个窝打印:


MyEnum:1

MyEnum:2

MyEnum:3


对我来说似乎不太实用。枚举根本不是用于传递给函数本身而不是使用他们的

底层类型的成员。


也许有人MVP能够为枚举提供一些启示。


问候


凯瑟琳



Unfortunately it is not possible to pass an enum to a method this way.
Since it is a special (user-defined) value type derived from
System.Enum, you need to pass the underlying type to the function.

For example, to make use of the enum''s members you will have to pass
them seperately specifying their underlying type:

private enum MyEnum : int { ONE = 1, TWO = 2, THREE = 3 }

private void DoIt(int i)
{
switch (i)
{
case 1:
Console.WriteLine("MyEnum: {0}", i);
break;
case 2:
Console.WriteLine("MyEnum: {0}", i);
break;
case 3:
Console.WriteLine("MyEnum: {0}", i);
break;
default:
break;
}
}

this.DoIt((int)MyEnum.ONE);
this.DoIt((int)MyEnum.TWO);
this.DoIt((int)MyEnum.THREE);

This would print:

MyEnum: 1
MyEnum: 2
MyEnum: 3

Seems not very practical to me. Enums are simply not intended to be
passed to functions itself rather than their members using their
underlying type.

Maybe someone of the MVPs is able to shed some light on enums.

Regards

Catherine




不幸的是,不可能以这种方式将枚举传递给方法。
因为它是从System派生的特殊(用户定义的)值类型.Enum,
你需要将基础类型传递给函数。

例如,要使用枚举的成员,你必须分别传递它们
指定他们的联合国derlying类型:

私有枚举MyEnum:int {ONE = 1,TWO = 2,THREE = 3}

私有无效DoIt(int i)
{
开关(i)
案例1:
Console.WriteLine(" MyEnum:{0}",i);
休息;
案例2:
Console.WriteLine(" MyEnum:{0}",i);
break;
案例3:
Console.WriteLine(" MyEnum:{ 0}",i);
休息;
默认:
休息;
}


this.DoIt((int )MyEnum.ONE);
this.DoIt((int)MyEnum.TWO);
this.DoIt((int)MyEnum.THREE);

这将打印:

MyEnum:1
MyEnum:2
MyEnum:3

对我来说似乎不太实用。 Enums根本不打算传递给函数本身而不是传递给它们使用其基础类型的成员。

也许MVP中的某个人能够对枚举有所了解。

问候

凯瑟琳



Unfortunately it is not possible to pass an enum to a method this way.
Since it is a special (user-defined) value type derived from System.Enum,
you need to pass the underlying type to the function.

For example, to make use of the enum''s members you will have to pass them
seperately specifying their underlying type:

private enum MyEnum : int { ONE = 1, TWO = 2, THREE = 3 }

private void DoIt(int i)
{
switch (i)
{
case 1:
Console.WriteLine("MyEnum: {0}", i);
break;
case 2:
Console.WriteLine("MyEnum: {0}", i);
break;
case 3:
Console.WriteLine("MyEnum: {0}", i);
break;
default:
break;
}
}

this.DoIt((int)MyEnum.ONE);
this.DoIt((int)MyEnum.TWO);
this.DoIt((int)MyEnum.THREE);

This would print:

MyEnum: 1
MyEnum: 2
MyEnum: 3

Seems not very practical to me. Enums are simply not intended to be passed
to functions itself rather than their members using their underlying type.

Maybe someone of the MVPs is able to shed some light on enums.

Regards

Catherine



这篇关于将枚举作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:22