本文介绍了c#枚举:他们可以使用java枚举的成员和函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在java中,可以给一个枚举一个构造函数以及成员变量和函数。
in java it is possible to give an enum a constructor as well as member variables and functions.
我想知道如果这样的事情可能在c#枚举中好。如果是,那么怎么样?
i was wondering if something like this is possible in c# enums as well. if so, how?
非常感谢!
推荐答案
只有这样做的方法是使用扩展方法,这可以使它看起来像枚举有成员方法。
The only way to do something similar to this is to use extension methods, which can make it appear as though the enumeration has member methods.
除此之外,你可以创建一个您的枚举的配套结构类型具有枚举值的属性,然后添加其他属性和方法来支持该值。
Other than that, you could create a companion struct type to your enumeration that has a property for the enumeration value and then adds additional properties and methods to support that value.
这篇关于c#枚举:他们可以使用java枚举的成员和函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!