[英]Methods in c# enums
In Java we can use methods in enums, for example i can write 在Java中,我们可以使用枚举中的方法,例如我可以编写
public static void main(String []args){
System.out.println(GetNum.TWO.get());
}
enum GetNum{
ONE{
public int get(){
return 1;
}
},
TWO{
public int get(){
return 2;
}
};
public abstract int get();
}
maybe somebody can say me: in c# enums can I do something like this? 也许有人可以说:在C#枚举中,我可以做这样的事情吗?
Not really, but you can sort of though the use of extension methods. 并非如此,但是您可以使用扩展方法。
Given an enumeration such as 给定一个枚举
enum HurfDurf
{
Hurr,
Durr
}
you can create an extension method such as 您可以创建一个扩展方法,例如
static class HurfDurfExtensions
{
public static string Wat(this HurfDurf lol)
{
return lol == HurfDurf.Hurr ? "Wew lad" : "eyy boss";
}
}
and use it like 并像这样使用
var whatisthisidonteven = HurfDurf.Hurr.Wat();
No you can't. 不,你不能。 In the background enum
are value-types (eg just an int). 在背景enum
是值类型(例如,仅是一个int)。
Eg you can do int i = (int)yourEnum;
例如,你可以做int i = (int)yourEnum;
and vice versa. 反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.