如何在C#中使用枚举 [英] How to you use Enums in C#

查看:97
本文介绍了如何在C#中使用枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

enum network
        {
            AIRTEL  = 0701,
            AIRTEL  = 0708,
            AIRTEL  = 0802,
            AIRTEL  = 0808,
            AIRTEL  = 0812,
            ETISALAT = 0809,
            ETISALAT = 0817,
            ETISALAT = 0818,
            ETISALAT = 0909,
            GLO = 0705,
            GLO = 0805,
            GLO = 0807,
            GLO = 0811,
            GLO = 0815,
            MTN = 0703,
            MTN = 0706,
            MTN = 0803,
            MTN = 0806,
            MTN = 0810,
            MTN = 0813,
            MTN = 0814,
            MTN = 0816,
            MTN = 0903
        }





如何根据变化值检查常数



How can i check the constants against the varying values

推荐答案

你确实意识到你的枚举例如,网络,不会编译?每个Enum条目都是一个名为Constant的,必须是唯一,并且必须是一个整数 value 类型:int,byte,long等。



这将编译:
You do realize your Enum example, "network," won't compile ? Each Enum entry is a named Constant which must be unique, and which must be an integral value type: int, byte, long, etc.

This will compile:
enum network
{
    Airtel,
    ETISALAT,
    GLO,
    MTN
}

并且,由于未指定初始化值或类型,因此基础类型的枚举条目为int,并且它们具有从默认值#0开始的相应数值,并按#1递增。这相当于:

And, since an initializing value is not specified, or a Type, the Enum entries underlying Type is int, and they have a corresponding numeric value starting at the default, #0, and incrementing by #1. This is equivalent to:

public static class NetWork
{
    public const int Airtel = 0;
    public const int ETISALAT = 1;
    public const int GLO = 2;
    public const int MTN = 3;
}

在您的情况下,您希望与网络中的每个枚举关联一个或多个值,这些值不是您可以表达哪些值与特定值相关联的任何顺序枚举。这表明这可能不是Enum有用的场景。



Enums有很多用途。我建议你从研究这些文章开始:



MSDN:[ ^ ],[ ^ ]。



CodeProject :[ ^ ], [ ^ ],[ ^ ]。



请详细描述你的意思是根据不同的值检查常数。

In your case you have one or more values you wish to associate with each Enum in 'network, and these are not in any order where you could express which values are associated with a specific Enum. That suggests this may not be a scenario in which an Enum is that useful.

There are many uses for Enums. I'd suggest you start by studying these articles:

MSDN: [^], [^].

CodeProject: [^], [^], [^].

Please describe with more detail what you mean by "check the constants against the varying values."


试试:

Try:
network net = network.AIRTEL;
...
if (net == network.AIRTEL)
   {
   ...
   }
...
switch (net)
   {
   case network.AIRTEL:
   case network.ETISALAT:
      break;
   }


好的,基于你的:我如何根据变量值检查常量语句,我要猜测你想要取这些常数值并将它们与枚举所代表的不同类别联系起来。

你可以尝试类似的东西:

OK, based on your: "How can i check the constants against the varying values" statement, I'm going to guess that you want to take these constant values and associate them to different categories represented by the enum.
You could try something like:
enum network
{
    Unknown = 0,    // it is usually a good idea to define some "Unknown" or "None" identifier to value == 0
    Airtel,
    ETISALAT,
    GLO,
    MTN
}
static readonly Dictionary<int, network> NetworkCodeToNetworkMap =
  new Dictionary<int, network>(){
             { 0701, network.AIRTEL },
             { 0708, network.AIRTEL },
             { 0802, network.AIRTEL },
             { 0808, network.AIRTEL },
             { 0812, network.AIRTEL },
             { 0809, network.ETISALAT },
             { 0817, network.ETISALAT },
             { 0818, network.ETISALAT },
             { 0909, network.ETISALAT },
             { 0705, network.GLO },
             { 0805, network.GLO },
             { 0807, network.GLO },
             { 0811, network.GLO },
             { 0815, network.GLO },
             { 0703, network.MTN },
             { 0706, network.MTN },
             { 0803, network.MTN },
             { 0806, network.MTN },
             { 0810, network.MTN },
             { 0813, network.MTN },
             { 0814, network.MTN },
             { 0816, network.MTN },
             { 0903  network.MTN }};



然后将数字与枚举关联:


Then to associate the number to the enum:

network AssociateToNetwork(int code)
{
  network association;
  if (NetworkCodeToNetworkMap.TryGetValue(code, out association))
  {
    //association will be set to the "correct" value as defined in the Dictionary
    return association;
  }
  // you can report an error/warning here that the code was unknown
  // association will already be set to the 0 value here
  return association;
}  


这篇关于如何在C#中使用枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆