Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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# 搜索枚举中的字符串数组,如果可用,则返回true_C#_Arrays_Sorting_Enums - Fatal编程技术网

C# 搜索枚举中的字符串数组,如果可用,则返回true

C# 搜索枚举中的字符串数组,如果可用,则返回true,c#,arrays,sorting,enums,C#,Arrays,Sorting,Enums,我列举了一个例子: [Flags] public enum MyColours{ Red = 1, Green = 2, Blue = 4, Yellow = 8, Orange = 16, }; 现在我有一个字符串列表: string[] colour = new string { "Red", "Orange", "Blue"}; 我希望能够为与枚举匹配的stirng返回true。您的问题非常模糊。但我想你是指这样

我列举了一个例子:

    [Flags]
    public enum MyColours{
    Red = 1,
    Green = 2, 
    Blue = 4,
    Yellow = 8,
    Orange = 16,
    };
现在我有一个字符串列表:

    string[] colour = new string { "Red", "Orange", "Blue"};

我希望能够为与枚举匹配的stirng返回true。

您的问题非常模糊。但我想你是指这样的事情

if (colour[0] == Enum.GetName(typeof(MyColors), 1)) //"Red" == "Red"
{
   return true;
}
Enum.GetName(typeof(MyColors),1)
就是您要找的


typeof(enumName)后跟enumIndex

更好的答案-更不冗长:

var values = Enum.GetNames(typeof(MyColours)).ToList();

string[] colour = new string[] { "Red", "Orange", "Blue" };

List<string> colourList = colours.ToList();

ConatainsAllItems(values, colourList);

public static bool ContainsAllItems(List<T> a, List<T> b)
{
    return !b.Except(a).Any();
}
var values=Enum.GetNames(typeof(myclours)).ToList();
字符串[]颜色=新字符串[]{“红色”、“橙色”、“蓝色”};
List colorlist=colors.ToList();
ConatainsAllItems(值、颜色列表);
公共静态布尔项(列表a、列表b)
{
return!b.除了(a).Any();
}

如果它们有匹配的值,则返回true。您可以使用
Enum.TryParse
方法,如下所示:

MyColours c;
from s in colour select new {Value = s, IsAvailable = Enum.TryParse(s, true, out c)}
或者,您可以执行
循环
,并使用该方法计算数组中每个值的值

这个问题很简单

我认为您想查看枚举中的值是否与参数匹配,并根据该参数返回true

bool IsInsideEnum(string value) {
  foreach (var enumVal in Enum.GetValues(typeof(MyColors)) 
    if(Enum.GetName(typeof(MyColors), enumVal) == value) 
      return true;
  return false;
}

非常直接,将枚举定义为:

   string[] colour = new string[] { "Red", "Orange", "Blue", "White" };

   var result = colour
     .Select(c => Enum.IsDefined(typeof(MyColours), c));
试验

List color=新列表{“红色”、“橙色”、“蓝色”};
List enumColors=Enum.GetNames(typeof(myclours)).ToList();
foreach(枚举颜色中的字符串)
{
如果(颜色存在(e=>e==s))
返回true;
其他的
返回false;
}
希望这有助于

使用
Enum.GetName(Enum,int)
获取枚举字符串

试试这个

foreach(string s in colour)
       { 
           Enum.GetNames(typeof(MyColours)).Contains(s); 
       }

你试过什么吗?问题是什么?您卡在哪里了?
为字符串返回true
-这一点都不清楚。如果它表示
枚举
中的值,是否要传入单个字符串并返回true?很抱歉,我不清楚。我是编程新手。在这里,我想写一个方法,它将参数作为颜色列表,并希望明确知道列表中的哪些颜色与枚举匹配,并为这些特定颜色放置一些像true或false这样的标志。这只会进入第一个循环上的if,假设他有一个循环。他的问题很模糊。我提供了他处理这个问题所需的代码。这个方法有点先进。看起来用户一开始不知道怎么做,linq selects可能需要等待。。。至少有一段时间
   List<string> colour = new List<String>{ "Red", "Orange", "Blue" };
   List<string> enumColors = Enum.GetNames(typeof(MyColours)).ToList();
   foreach (string s in enumColors)
   {
          if (colour.Exists(e => e == s))
              return true;
          else
              return false;

   }
foreach(string s in colour)
       { 
           Enum.GetNames(typeof(MyColours)).Contains(s); 
       }