简体   繁体   English

如何在 C# 中获取下一个(或上一个)枚举值

[英]How to get next (or previous) enum value in C#

I have an enum which is defined like this:我有一个这样定义的枚举:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个eRat.C

The solution I see is (without range checking)我看到的解决方案是(没有范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在这太复杂了,对于这么简单的事情。 Do you know any better solution??你知道更好的解决方案吗?? Something like eRat.B+1 or Enum.Next(Erat.B) ?eRat.B+1Enum.Next(Erat.B)类的东西?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

I have an enum which is defined like this:我有一个枚举定义如下:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B , I want to get the next one which is eRat.C所以给定值eRat.B ,我想得到下一个是eRat.C

The solution I see is (without range checking)我看到的解决方案是(不进行范围检查)

Array a = Enum.GetValues(typeof(eRat));
int i=0 ;
for (i = 0; i < a.GetLength(); i++)
{
       if (a.GetValue(i) == eRat.B)
            break;
}
return (eRat)a.GetValue(i+1):

Now that is too much complexity, for something that simple.现在,对于这么简单的事情来说,这太复杂了。 Do you know any better solution??您知道更好的解决方案吗? Something like eRat.B+1 or Enum.Next(Erat.B) ?类似于eRat.B+1Enum.Next(Erat.B)吗?

Thanks谢谢

Based on best answer from Yahya Hussein here is edit of his code for Previous element in Enum基于来自 Yahya Hussein 的最佳答案,这里是他的 Enum 中前一个元素的代码的编辑

public static class Extensions
{
    public static T Previous<T>(this T src) where T : struct
    {
        if (!typeof(T).IsEnum) throw new ArgumentException(String.Format("Argument {0} is not an Enum", typeof(T).FullName));

        T[] Arr = (T[])Enum.GetValues(src.GetType());
        int j = Array.IndexOf<T>(Arr, src) - 1;
        return (j < 0) ? Arr[Array.Length - 1] : Arr[j];            
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM