如何检查数字是否是c#中列表的元素?

如何检查数字是否是c#中列表的元素?,c#,C#,在Python中,我可以做到这一点: if 1 in [1, 2, 3, 1]: print("The number 1 is an element of this list.") else: print("The number 1 is not an element of this list.") 我想用c#做一些类似的事情。到目前为止,我一直在这样做: using System; using System.Collections.Generic; namespace Ch

在Python中,我可以做到这一点:

if 1 in [1, 2, 3, 1]:
    print("The number 1 is an element of this list.")
else:
    print("The number 1 is not an element of this list.")
我想用c#做一些类似的事情。到目前为止,我一直在这样做:

using System;
using System.Collections.Generic;

namespace CheckMembership
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> myList = new List<int>() { 1, 2, 3, 1 };

            for (int i = 0; i < myList.Count; i++)
            {
                if (myList[i] == 1)
                {
                    Console.WriteLine("The number 1 is an element of this list.");
                    break;
                }

                if (i == myList.Count - 1 && myList[i] != 1)
                    Console.WriteLine("The number 1 is not an element of this list.");
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
命名空间检查成员资格
{
班级计划
{
静态void Main(字符串[]参数)
{
List myList=newlist(){1,2,3,1};
for(int i=0;i

有没有一种更简洁、更有效的方法,也许没有循环?

您可以使用
Contains()

使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间控制台EAPP1
{
班级计划
{
静态void Main(字符串[]参数)
{
控制台。写入(“键入数字:”;
int.TryParse(Console.ReadLine(),out int searchItem);
列表项=新列表(){1,2,3,1};
//确定该项是否在列表中。
Console.WriteLine($“{searchItem}{(items.Contains(searchItem)?“is”:“is not”)}在列表中);
WriteLine($“{searchItem}{(items.Where(item=>item==searchItem.Any()?”是“:”不是“}在列表中”);
WriteLine($“{searchItem}{(items.Where(item=>item==searchItem.Count()>0?“是”:“不是”)}在列表中);
//确定发生的次数
WriteLine($“{searchItem}在列表中找到{items.Where(item=>item==searchItem.Count()}次”);
Console.ReadLine();
}
}
}

很清楚,这样就完全不需要循环,而且可以删除它。您的循环解决方案是一个很好的开始,但可能需要一些工作。你能用这个签名写一个方法吗?
static bool Contains(List items,int-candidate){…}
如果候选者在列表中,则返回
true
,否则返回
false
?如果您可以这样做,那么您可以使
Main
方法更易于阅读
if(Contains(myList,1))…
作为一个初学者,总是要问自己“我能把这个功能提取到一个能很好地解决一个问题的方法中吗?”这就是我们制作方法库的方法,这些方法组合起来可以解决更大的问题。现在,正如答案所指出的,已经有人为您编写了这个方法。但是,通过自己编写“基本”方法作为学习练习,您将看到编写这些库方法并不是一种魔术;它们是由开发人员编写的,您也可以是这些开发人员中的一员。此外,我注意到您的解决方案并不完全正确。例如,如果您的列表为空,会发生什么情况?它应该产生这样一条消息:
1不是元素
,因为列表是空的。但是你的程序没有这样做。你能看看如何解决这个问题吗?谢谢你,埃里克。事实上,我是一个初学者,只是把编码作为一种爱好,所以我觉得你的指导方针非常有用。我确实喜欢编写自己的方法作为学习工具,然后我会寻找现有的方法,使我的代码看起来更好,工作得更好。
if (myList.Contains(1))
{
}
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Type a number : ");
            int.TryParse(Console.ReadLine(), out int searchItem);
            List<int> items = new List<int>() { 1, 2, 3, 1 };

            //Determine if the item is in the list.
            Console.WriteLine($"{searchItem} {(items.Contains(searchItem) ? "is" : "isn't")} in the list");
            Console.WriteLine($"{searchItem} {(items.Where(item => item == searchItem).Any() ? "is" : "isn't")} in the list");
            Console.WriteLine($"{searchItem} {(items.Where(item => item == searchItem).Count() > 0 ? "is" : "isn't")} in the list");

            //Determine number of ocurrences
            Console.WriteLine($"{searchItem} is found {items.Where(item => item == searchItem).Count()} times in the list");

            Console.ReadLine();
        }
    }
}