📜  C#|从列表中删除一系列元素

📅  最后修改于: 2021-05-30 00:16:34             🧑  作者: Mango

List .RemoveRange(Int32,Int32)方法用于从List 中移除一系列元素。

列表的属性:

  • 它与数组不同。列表可以动态调整大小,但数组则不能。
  • 列表类可以接受null作为引用类型的有效值,并且还允许重复的元素。
  • 如果计数通过重新分配内部数组自动变成等于容量则的列表的容量增加。在添加新元素之前,现有元素将被复制到新数组。

句法:

public void RemoveRange (int index, int count);

参数:

例外情况:

  • ArgumentOutOfRangeException:如果索引小于零或Count小于零。
  • ArgumentException:如果索引和Count未表示List 中的有效范围的元素。

下面的程序说明了List .RemoveRange(Int32,Int32)方法的用法:

范例1:

// C# Program to remove a range of
// elements from the List
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of strings
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add("Geeks");
        firstlist.Add("For");
        firstlist.Add("Geeks");
        firstlist.Add("GFG");
        firstlist.Add("C#");
        firstlist.Add("Tutorials");
        firstlist.Add("GeeksforGeeks");
  
        // Displaying the elements of firstlist
        Console.WriteLine("Elements in List:\n");
  
        foreach(string ele in firstlist)
        {
            Console.WriteLine(ele);
        }
  
        // removing 1 elements starting
        // from index 3
        firstlist.RemoveRange(3, 1);
  
        Console.WriteLine("");
  
        // Displaying the updated List
        Console.WriteLine("After Removing of elements:\n");
  
        // Displaying the elements in List
        foreach(string ele in firstlist)
        {
            Console.WriteLine(ele);
        }
    }
}

输出:

Elements in List:

Geeks
For
Geeks
GFG
C#
Tutorials
GeeksforGeeks

After Removing of elements:

Geeks
For
Geeks
C#
Tutorials
GeeksforGeeks

范例2:

// C# Program to remove a range of
// elements from the List
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add(1);
        firstlist.Add(2);
        firstlist.Add(3);
        firstlist.Add(4);
        firstlist.Add(5);
        firstlist.Add(6);
        firstlist.Add(7);
  
        // Displaying the elements of firstlist
        Console.WriteLine("Elements in List:\n");
  
        foreach(int ele in firstlist)
        {
            Console.WriteLine(ele);
        }
  
        // removing 2 elements starting
        // from index 3 i.e 3rd and 4th
        firstlist.RemoveRange(3, 2);
  
        Console.WriteLine("");
  
        // Displaying the updated List
        Console.WriteLine("After Removing of elements:\n");
  
        // Displaying the elements in List
        foreach(int ele in firstlist)
        {
            Console.WriteLine(ele);
        }
    }
}

输出:

Elements in List:

1
2
3
4
5
6
7

After Removing of elements:

1
2
3
6
7

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.removerange?view=netframework-4.7.2