Can Enum contains other Enum elements plus its own elements ?
public enum CardSuit { spades, hearts, diamonds, clubs } public enum GameSuit { spades, // These are hearts, // the same diamonds, // " CardSuit " clubs, // elements suns // " GameSuit " special element } Can we include CardSuit in GameSuit without redundant retyping same elements ?
Answer: 1
What you have typed is legal but both enums are independent of each other.
GameSuit enum spades has no connection with CardsSuit enum spades.
Answer: 2
Sadly no, there's no good solution for what you want using enums. There are other options you can try, such as a series of public static readonly fields of a particular "enum-like" type:
public class CardSuit { public static readonly CardSuit Spades = new CardSuit(); public static readonly CardSuit Hearts = new CardSuit(); ... } public enum GameSuit : CardSuit { public static readonly GameSuit Suns = new GameSuit(); } In practice, this can work mostly as well, albeit without switch statement support. Usage might be like:
var cardSuit = ...; if (cardSuit == CardSuit.Spades || cardSuit == GameSuit.Suns) { ... } by : Kirk Wollhttp://stackoverflow.com/users/189950Answer: 3
You can carry values from one enumerated type to another explicitly:
public enum Foo { a, b, c } public enum Bar { a = Foo.a, b = Foo.b, c = Foo.c + 3, d } by : HABOhttp://stackoverflow.com/users/92546Answer: 4
Enum is a special class, with special behavior. You can't directly inherit this class, but it's inderectly inheriter any time you declare a new enum.
You cannot inherit one new enum from other enum.
The only way you can do something like that, would be using reflection. But you don't get what you want to do. This can so how far you can get using Refletcion with enums:
But I'm afraid this doesn't server your purpose.
by : JotaBehttp://stackoverflow.com/users/1216612
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog