获取C#中List 中包含的元素数
给定一个列表,我们必须使用List.Countproperty来计算其元素总数。
C#列表
列表用于表示对象列表,它表示为List<T>,其中T是列表对象/元素的类型。
列表是System.Collections.Generic包中的一个类,因此我们必须首先包含它。
List.Count属性
Count是List类的属性;它返回List的元素总数。
语法:
List_name.Count;
在此,List_name是要计算其元素的输入/源列表的名称。
示例
Input:
//整数列表
List<int> int_list = new List<int> { 10, 20, 30, 40, 50, 60, 70 };
//字符串列表
List<string> str_list = new List<string>{
"Manju", "Amit", "Abhi", "Radib", "Prem"
};
Function call:
int_list.Count;
str_list.Count;
Output:
7
5C#程序计算列表中元素的总数
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//整数列表
List<int> int_list = new List<int> { 10, 20, 30, 40, 50, 60, 70 };
//字符串列表
List<string> str_list = new List<string>{
"Manju", "Amit", "Abhi", "Radib", "Prem"
};
//打印元素总数
Console.WriteLine("int_list中的总元素数为: " + int_list.Count);
Console.WriteLine("str_list中的总元素数为: " + str_list.Count);
//按ENTER退出
Console.ReadLine();
}
}
}输出结果
int_list中的总元素数为: 7 str_list中的总元素数为: 5
热门推荐
10 哥哥中考加油祝福语简短
11 猪年幽默祝福语简短
12 妈妈生日贺卡祝福语 简短
13 高考给舍友祝福语简短
14 关云生字祝福语简短
15 科研文案祝福语简短励志
16 二胎祝福语简短霸气
17 祝福语对联文案简短大气
18 简短佛教新年祝福语大全