Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将枚举类型转换为IList_C#_Ienumerable_Menu_Ilist - Fatal编程技术网

C# 将枚举类型转换为IList

C# 将枚举类型转换为IList,c#,ienumerable,menu,ilist,C#,Ienumerable,Menu,Ilist,你好 如何将enum转换为IList? 假设您要读取FormWindowsStateenum中的所有元素,并返回一个IList,其中包含Normal、Minimized和MaximizedUse。例如: var list = (IList<FormWindowState>) Enum.GetValues(typeof(FormWindowState)); var list=(IList)Enum.GetValues(typeof(formWindowsState)); 它实际上返

你好
如何将
enum
转换为
IList

假设您要读取
FormWindowsState
enum中的所有元素,并返回一个
IList
,其中包含
Normal
Minimized
Maximized
Use。例如:

var list = (IList<FormWindowState>) Enum.GetValues(typeof(FormWindowState));
var list=(IList)Enum.GetValues(typeof(formWindowsState));
它实际上返回了一个
FormWindowsState[]
,但无论如何它都适当地实现了
IList

如果您正在使用enum进行大量工作,并且希望使用更安全的方法,那么您也可以查看我的项目。

使用。例如:

var list = (IList<FormWindowState>) Enum.GetValues(typeof(FormWindowState));
IList<FormWindowState> myList;//instantiate it with a concrete collection.


myList.Add(FormWindowsState.Normal);
myList.Add(FormWindowsState.Minimized);
myList.Add(FormWindowsState.Maximized);
var list=(IList)Enum.GetValues(typeof(formWindowsState));
它实际上返回了一个
FormWindowsState[]
,但无论如何它都适当地实现了
IList

如果您正在使用enum进行大量工作,并且希望使用更安全的方法,那么您可能也希望查看我的项目。

IList myList//用一个具体的集合实例化它。
IList<FormWindowState> myList;//instantiate it with a concrete collection.


myList.Add(FormWindowsState.Normal);
myList.Add(FormWindowsState.Minimized);
myList.Add(FormWindowsState.Maximized);
myList.Add(FormWindowsState.Normal); 添加(FormWindowsState.Minimized); 添加(FormWindowsState.Maximized);
这只是针对这种情况,当您只有3个枚举成员时。否则使用Jon Skeet的溶液。

IList myList//用一个具体的集合实例化它。
myList.Add(FormWindowsState.Normal);
添加(FormWindowsState.Minimized);
添加(FormWindowsState.Maximized);

这只是针对这种情况,当您只有3个枚举成员时。否则,请使用Jon Skeet的解决方案。

如果您确实希望显式添加它们,至少使用集合初始值设定项而不是显式添加调用会更好。是的,您是对的,我仍然主要使用.NET 2.0,在使用新的语言功能时没有使用太多+1供评论。只要使用C#3编译器编译,您仍然可以在.NET2.0中使用集合初始值设定项。如果您使用的是VS2005,我很同情您:(如果您确实希望显式添加它们,那么至少使用集合初始值设定项而不是显式添加调用会更好。是的,您是对的,我仍然主要使用.NET 2.0,并且没有太多使用新的语言功能。+1也用于注释。只要您正在构建,您仍然可以在.NET 2.0中使用集合初始值设定项。)使用C#3编译器。如果您使用的是VS2005,我会同情您:(使用:var list=(IList)Enum.GetValues(typeof(formWindowsState)).Cast()不是更好吗;使用:var list=(IList)Enum.GetValues(typeof(formWindowsState)).Cast()不是更好吗;