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)