我有一门课叫问题(复数)。在这个类中有一个名为Question(单数)的枚举,看起来像这样。
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
在Questions类中,我有一个get(intfoo)函数,它为该foo返回Questions对象。有没有一种简单的方法可以从枚举中获取整数值,这样我就可以执行类似于Questions.get(Questions.Role)的操作?
只需转换枚举,例如。
int something = (int) Question.Role;
以上内容适用于您在野外看到的绝大多数枚举,因为枚举的默认基础类型是int。
然而,正如cecilphilip所指出的,遗尿症可能有不同的潜在类型。如果枚举声明为uint、long或ulong,则应将其强制转换为枚举的类型;例如,用于
enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649};
你应该使用
long something = (long)StarsInMilkyWay.Wolf424B;
例子:
public enum EmpNo
{
Raj = 1,
Rahul,
Priyanka
}
在后面的代码中获取枚举值:
int setempNo = (int)EmpNo.Raj; // This will give setempNo = 1
or
int setempNo = (int)EmpNo.Rahul; // This will give setempNo = 2
枚举将递增1,您可以设置起始值。如果不设置起始值,初始值将指定为0。
为了确保枚举值存在,然后解析它,还可以执行以下操作。
// Fake Day of Week
string strDOWFake = "SuperDay";
// Real Day of Week
string strDOWReal = "Friday";
// Will hold which ever is the real DOW.
DayOfWeek enmDOW;
// See if fake DOW is defined in the DayOfWeek enumeration.
if (Enum.IsDefined(typeof(DayOfWeek), strDOWFake))
{
// This will never be reached since "SuperDay"
// doesn't exist in the DayOfWeek enumeration.
enmDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDOWFake);
}
// See if real DOW is defined in the DayOfWeek enumeration.
else if (Enum.IsDefined(typeof(DayOfWeek), strDOWReal))
{
// This will parse the string into it's corresponding DOW enum object.
enmDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDOWReal);
}
// Can now use the DOW enum object.
Console.Write("Today is " + enmDOW.ToString() + ".");
如果您想为存储在变量中的枚举值获取一个整数(其类型为Question),以便在方法中使用,您可以简单地执行我在本例中编写的操作:
enum Talen
{
Engels = 1, Italiaans = 2, Portugees = 3, Nederlands = 4, Duits = 5, Dens = 6
}
Talen Geselecteerd;
public void Form1()
{
InitializeComponent()
Geselecteerd = Talen.Nederlands;
}
// You can use the Enum type as a parameter, so any enumeration from any enumerator can be used as parameter
void VeranderenTitel(Enum e)
{
this.Text = Convert.ToInt32(e).ToString();
}
这会将窗口标题更改为4,因为变量Geselecterd是Talen.Nedelands。如果我将其更改为Talen.Portuges并再次调用该方法,文本将更改为3。
我能想到的最简单的解决方案是重载Get(int)方法,如下所示:
[modifiers] Questions Get(Question q)
{
return Get((int)q);
}
其中[modifiers]通常可以与Get(int)方法相同。如果无法编辑Questions类或出于某种原因不想编辑,可以通过编写扩展来重载该方法:
public static class Extensions
{
public static Questions Get(this Questions qs, Question q)
{
return qs.Get((int)q);
}
}
最近,我不再在代码中使用枚举,而是使用带有受保护构造函数和预定义静态实例的类(感谢Roelof-C#确保有效枚举值-Futureproof方法)。
鉴于此,下面是我现在如何处理这个问题(包括隐式转换为int或从int转换)。
public class Question
{
// Attributes
protected int index;
protected string name;
// Go with a dictionary to enforce unique index
//protected static readonly ICollection<Question> values = new Collection<Question>();
protected static readonly IDictionary<int,Question> values = new Dictionary<int,Question>();
// Define the "enum" values
public static readonly Question Role = new Question(2,"Role");
public static readonly Question ProjectFunding = new Question(3, "Project Funding");
public static readonly Question TotalEmployee = new Question(4, "Total Employee");
public static readonly Question NumberOfServers = new Question(5, "Number of Servers");
public static readonly Question TopBusinessConcern = new Question(6, "Top Business Concern");
// Constructors
protected Question(int index, string name)
{
this.index = index;
this.name = name;
values.Add(index, this);
}
// Easy int conversion
public static implicit operator int(Question question) =>
question.index; //nb: if question is null this will return a null pointer exception
public static implicit operator Question(int index) =>
values.TryGetValue(index, out var question) ? question : null;
// Easy string conversion (also update ToString for the same effect)
public override string ToString() =>
this.name;
public static implicit operator string(Question question) =>
question?.ToString();
public static implicit operator Question(string name) =>
name == null ? null : values.Values.FirstOrDefault(item => name.Equals(item.name, StringComparison.CurrentCultureIgnoreCase));
// If you specifically want a Get(int x) function (though not required given the implicit converstion)
public Question Get(int foo) =>
foo; //(implicit conversion will take care of the conversion for you)
}
这种方法的优点是你可以从枚举中得到所有的东西,但是你的代码现在更灵活了,所以如果你需要根据Question的值执行不同的操作,你可以将逻辑放入Question本身(即以首选的OO方式),而不是在代码中放入大量的case语句来处理每个场景。
注意:2018-04-27更新了答案,以利用C#6功能;即声明表达式和lambda表达式体定义。原始代码参见修订历史。这样做的好处是使定义不那么冗长;这是对这个答案方法的主要抱怨之一。
public enum QuestionType
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
…是一个很好的声明。
您必须将结果强制转换为int,如下所示:
int Question = (int)QuestionType.Role
否则,类型仍然是QuestionType。
这种严格程度是C#的方式。
一种替代方法是改用类声明:
public class QuestionType
{
public static int Role = 2,
public static int ProjectFunding = 3,
public static int TotalEmployee = 4,
public static int NumberOfServers = 5,
public static int TopBusinessConcern = 6
}
声明不那么优雅,但不需要将其转换为代码:
int Question = QuestionType.Role
或者,您可能会对Visual Basic感到更舒服,它在许多方面都满足了这种期望。
也许我错过了,但有人尝试过简单的通用扩展方法吗?
这对我来说非常有用。您可以通过这种方式避免API中的类型转换,但最终会导致更改类型操作。这是对Roslyn编程的一个很好的例子,让编译器为您生成GetValue<T>方法。
public static void Main()
{
int test = MyCSharpWrapperMethod(TestEnum.Test1);
Debug.Assert(test == 1);
}
public static int MyCSharpWrapperMethod(TestEnum customFlag)
{
return MyCPlusPlusMethod(customFlag.GetValue<int>());
}
public static int MyCPlusPlusMethod(int customFlag)
{
// Pretend you made a PInvoke or COM+ call to C++ method that require an integer
return customFlag;
}
public enum TestEnum
{
Test1 = 1,
Test2 = 2,
Test3 = 3
}
}
public static class EnumExtensions
{
public static T GetValue<T>(this Enum enumeration)
{
T result = default(T);
try
{
result = (T)Convert.ChangeType(enumeration, typeof(T));
}
catch (Exception ex)
{
Debug.Assert(false);
Debug.WriteLine(ex);
}
return result;
}
}
我最喜欢的int或更小enums黑客:
GetHashCode();
对于枚举
public enum Test
{
Min = Int32.MinValue,
One = 1,
Max = Int32.MaxValue,
}
这
var values = Enum.GetValues(typeof(Test));
foreach (var val in values)
{
Console.WriteLine(val.GetHashCode());
Console.WriteLine(((int)val));
Console.WriteLine(val);
}
输出
one
1
1
max
2147483647
2147483647
min
-2147483648
-2147483648
免责声明:
它不适用于基于long的枚举。
public enum Suit : int
{
Spades = 0,
Hearts = 1,
Clubs = 2,
Diamonds = 3
}
Console.WriteLine((int)(Suit)Enum.Parse(typeof(Suit), "Clubs"));
// From int
Console.WriteLine((Suit)1);
// From a number you can also
Console.WriteLine((Suit)Enum.ToObject(typeof(Suit), 1));
if (typeof(Suit).IsEnumDefined("Spades"))
{
var res = (int)(Suit)Enum.Parse(typeof(Suit), "Spades");
Console.Out.WriteLine("{0}", res);
}
你应该使用我们可以在任何其他语言中使用的类型铸造。
如果枚举是这样的-
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
你需要转换为int,然后这样做-
Question q = Question.Role;
.............
.............
int something = (int) q;
Re-
在C#中,有两种类型的铸件:
隐式转换(自动)-将较小的类型转换为较大的类型大小,如-
char->int->long->float->double
显式转换(手动)-将较大的类型转换为较小的类型,如-
double->float->long->int->char
更多信息可以在这里找到。