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 20岁简短生日祝福语
17 喜事敬酒祝福语简短精辟
18 舅妈生日红包祝福语简短