Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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# 通用列表中的变量数_C#_List_Generics_Methods - Fatal编程技术网

C# 通用列表中的变量数

C# 通用列表中的变量数,c#,list,generics,methods,C#,List,Generics,Methods,我想知道列表中指示对象数量是否满足特定要求的通用方法(如果有),例如: List<string> example = new List<string>(); if (example."put the method here" = 0) { Console.WriteLine("There are no objects in this list"); } else if (example."put method here" > 0) { Consol

我想知道列表中指示对象数量是否满足特定要求的通用方法(如果有),例如:

List<string> example = new List<string>();

if (example."put the method here" = 0)
{
    Console.WriteLine("There are no objects in this list");
}
else if (example."put method here" > 0)
{
    Console.WriteLine("This list contains objects");
列表示例=新列表();
如果(例如,“将方法放在此处”=0)
{
Console.WriteLine(“此列表中没有对象”);
}
else if(例如,“此处放置方法”>0)
{
WriteLine(“此列表包含对象”);

在示例代码中,我想知道这个列表是否包含0个对象,然后控制台写入一个特定的文本,如果列表包含的项目/对象超过0个,则控制台写入另一个文本。

如果您不想在列表中看到任何项目,则只需使用
Count
(假设C是语言)。下面是您的代码的外观:

List<string> example = new List<string>();

if (example.Count == 0)
{
    Console.WriteLine("There are no objects in this list");
}
else if (example.Count > 0)
{
    Console.WriteLine("This list contains objects");
}

不使用Count()==0,您可以使用Any(),这样您就不用键入几个字符了!然后可以将“else if”更改为“else”。

请添加您正在使用的语言(我猜是C)
int count = example.Count(i => i.StartsWith("The") == true)