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 娘家除夕祝福语大全简短