C#中的List .RemoveRange()方法以及示例
C#List<T>.RemoveRange()方法
C#List<T>.RemoveRange()方法用于从列表中删除一系列元素。
语法:
void List<T>.RemoveRange(int index, int count);
参数:它接受两个参数1)index–起始位置和2)count–要从索引中删除的元素总数。
返回值:不返回任何内容–返回的类型为void
示例
int list declaration:
List<int> a = new List<int>();
adding elements:
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
removing elements:
////将从索引0中删除2个元素
a.RemoveRange(0, 2);
Output:
30 40 50C#使用List<T>.RemoveRange()方法从列表中删除项目的示例
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void printList(List<int> lst)
{
//打印元素
foreach (int item in lst)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//整数列表
List<int> a = new List<int>();
//添加元素
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
//打印列表
Console.WriteLine("list elements...");
printList(a);
//删除元素
//将从索引0中删除2个元素
a.RemoveRange(0, 2);
//删除元素后列出
Console.WriteLine("删除元素后列出元素。。。");
printList(a);
//按ENTER退出
Console.ReadLine();
}
}
}输出结果
list elements... 10 20 30 40 50 删除元素后列出元素。。。 30 40 50
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短