比较枚举和整数

请参考以下枚举


public enum Fruit

{

 Apple = 1,

 Orange = 2,

 Banana= 3

}


public enum Color

{

 Orange = 1,

 Yellow = 2,

 Red= 3

}

现在我想用颜色映射水果。所以我实现了


public enum FruitColor

{

 1= 3,

 2= 1,

 3= 2

}

实现 FruitColor 时出现语法错误


预期标识符


如何解决这个问题?


慕娘9325324
浏览 185回答 3
3回答

天涯尽头无女友

使用 anenum来映射枚举值毫无意义。我会用字典:Dictionary<Fruit,&nbsp;Color>&nbsp;FruitToColor&nbsp;=&nbsp;new&nbsp;Dictionary<Fruit,&nbsp;Color> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;{&nbsp;Fruit.Apple,&nbsp;Color.Red&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;{&nbsp;Fruit.Orange,&nbsp;Color.Orange&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&nbsp;{&nbsp;Fruit.Banana,&nbsp;Color.Yellow&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; Color&nbsp;colorOfBanana&nbsp;=&nbsp;FruitToColor[Fruit.Banana];&nbsp;//&nbsp;yields&nbsp;Color.Yellow

千巷猫影

也只是把它放在那里,因为我可以,唯一的好处是你可以在自定义属性中编码其他数据。但是,我会使用字典或开关;)给定的public enum MyFruit{&nbsp; &nbsp; [MyFunky(MyColor.Orange)]&nbsp; &nbsp; Apple = 1,&nbsp; &nbsp; [MyFunky(MyColor.Yellow)]&nbsp; &nbsp; Orange = 2,&nbsp; &nbsp; [MyFunky(MyColor.Red)]&nbsp; &nbsp; Banana = 3}public enum MyColor{&nbsp; &nbsp; Orange = 1,&nbsp; &nbsp; Yellow = 2,&nbsp; &nbsp; Red = 3}public static class MyExteions{&nbsp; &nbsp; public static MyColor GetColor(this MyFruit fruit)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var type = fruit.GetType();&nbsp; &nbsp; &nbsp; &nbsp; var memInfo = type.GetMember(fruit.ToString());&nbsp; &nbsp; &nbsp; &nbsp; var attributes = memInfo[0].GetCustomAttributes(typeof (MyFunkyAttribute), false);&nbsp; &nbsp; &nbsp; &nbsp; if (attributes.Length > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ((MyFunkyAttribute)attributes[0]).Color;&nbsp; &nbsp; &nbsp; &nbsp; throw new InvalidOperationException("blah");&nbsp; &nbsp; }}public class MyFunkyAttribute : Attribute{&nbsp; &nbsp; public MyFunkyAttribute(MyColor color) { Color = color;}&nbsp; &nbsp;&nbsp; &nbsp; public MyColor Color { get; protected set; }}用法var someFruit = MyFruit.Apple;var itsColor = someFruit.GetColor();Console.WriteLine("Fruit = " + someFruit + ", Color = " + itsColor);输出Fruit = Apple, Color = Orange

婷婷同学_

成员标识符不允许以数值开头,但是您可以使用一种方法从每个值中获取正确的值enum:public Fruit GetFruit(this Color c) {&nbsp; &nbsp; switch ((int)c) {&nbsp; &nbsp; &nbsp; &nbsp; case 1: return (Fruit)3;&nbsp; &nbsp; &nbsp; &nbsp; case 2: return (Fruit)2;&nbsp; &nbsp; &nbsp; &nbsp; case 3: return (Fruit)1;&nbsp; &nbsp; }&nbsp; &nbsp; return 0;}这种方法的逆过程会给你Color来自Fruit. 您可以通过Color类型调用此方法作为静态方法:Fruit myFruit = Color.GetFruit(Color.Orange);
打开App,查看更多内容
随时随地看视频慕课网APP