zoukankan      html  css  js  c++  java
  • c#遍历枚举里面的内容-获取枚举数量,取得大小

    private void ScanAllPLCStatus()
            {
               foreach (OpcUtil.PLC_Sensor_Enum e in Enum.GetValues(typeof(OpcUtil.PLC_Sensor_Enum)))
                {
                    Console.WriteLine(e.ToString());
                  //  bool sensor_result = OpcUtil.GetSensorStatus(e);
                }
            }

     https://blog.csdn.net/weixin_34112208/article/details/94122088

    方法一
    用映射来取得enum中的值
    
    public enum enums
    {
        a,
        b,
        c,
    }
    
    public static void GetEnumCount()
    {
        foreach (enums e in Enum.GetValues(typeof(enums)))
        {
            Debug.Log(e);
        }
    }
    方法二
    在结尾插入一个表示结束的枚举,来取得大小,这样就避免了映射的GC开销,很精巧的方法。
    
    public enum enums
    {
        a,
        b,
        c,
        End,
    }
    public static void GetEnumCount()
    {
        for (int i = 0; i < (int)enums.End; i++)
        {
            Debug.Log((enums)i);
        }
    }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    一切都是对象
    对象入门
    同步计算输入的各个数的总和与平均值
    与时间有关的类Date,DateFormat,Calendar
    获取文件信息
    串行化
    分解
    高速缓存
    压缩
    MyCAT实现MySQL的读写分离
  • 原文地址:https://www.cnblogs.com/txwtech/p/15343464.html
Copyright © 2011-2022 走看看