迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

C# 从列表中删除元素

作者:迹忆客 最近更新:2024/01/03 浏览次数:

在 C# 中,我们可以对 List 数据结构执行几种操作。可以添加、删除和替换这些元素。要在 C# 中从列表中删除一个元素,我们使用 Remove()RemoveAt()RemoveRange() 方法。

这些方法根据索引或值将其从列表中删除。在接下来的几个示例中,你将学习如何实现这些。


C# 使用 Remove() 方法从 List 中删除元素

这个 Remove() 方法根据列表上的名称删除元素。此方法的正确语法如下:

ListName.Remove("NameOfItemInList");

示例代码:

using System;
using System.Collections.Generic;

public class Removal {
  public static void Main() {
    List<string> Flowers = new List<string>();
    Flowers.Add("Rose");
    Flowers.Add("Jasmine");
    Flowers.Add("Lili");
    Flowers.Add("Hibiscus");
    Flowers.Add("Daisy");
    Console.WriteLine("List Before Removal:");
    foreach (string flower in Flowers) {
      Console.WriteLine(flower);
    }
    Console.WriteLine();
    // Use of Remove() method
    Flowers.Remove("Lili");
    Console.WriteLine("List After Removal:");
    foreach (string flower in Flowers) {
      Console.WriteLine(flower);
    }
  }
}

输出:

List Before Removal:
Rose
Jasmine
Lili
Hibiscus
Daisy

List After Removal:
Rose
Jasmine
Hibiscus
Daisy

C# 使用 RemoveAt() 方法从 List 中删除元素

RemoveAt() 方法根据该元素的索引号从 List 中删除该元素。我们已经知道 C# 中的索引以 0 开头。因此,选择索引号时要小心。此方法的正确语法如下:

ListName.RemoveAt(Index);

示例代码:

using System;
using System.Collections.Generic;

public class Removal {
  public static void Main() {
    List<string> Flowers = new List<string>();
    Flowers.Add("Rose");
    Flowers.Add("Jasmine");
    Flowers.Add("Lili");
    Flowers.Add("Hibiscus");
    Flowers.Add("Daisy");
    Console.WriteLine("List Before Removal:");
    foreach (string flower in Flowers) {
      Console.WriteLine(flower);
    }
    Console.WriteLine();
    // Use of RemoveAt() method
    Flowers.RemoveAt(3);
    Console.WriteLine("List After Removal:");
    foreach (string flower in Flowers) {
      Console.WriteLine(flower);
    }
  }
}

输出:

List Before Removal:
Rose
Jasmine
Lili
Hibiscus
Daisy

List After Removal:
Rose
Jasmine
Lili
Daisy

C# 使用 RemoveRange() 方法从 List 中删除元素

在 C# 中,我们也可以同时删除多个元素。为此,可以使用 RemoveRange() 方法。我们将要删除的元素范围作为参数传递给该方法。此方法的正确语法如下:

ListName.RemoveRange(int index, int count);

index 是要删除的元素的起始索引,count 是要删除的元素的数量。

示例代码:

using System;
using System.Collections.Generic;

public class Removal {
  public static void Main() {
    List<string> Flowers = new List<string>();
    Flowers.Add("Rose");
    Flowers.Add("Jasmine");
    Flowers.Add("Lili");
    Flowers.Add("Hibiscus");
    Flowers.Add("Daisy");
    Console.WriteLine("List Before Removal:");
    foreach (string flower in Flowers) {
      Console.WriteLine(flower);
    }
    Console.WriteLine();
    // Use of RemoveRange() method
    Flowers.RemoveRange(3, 2);
    Console.WriteLine("List After Removal:");
    foreach (string flower in Flowers) {
      Console.WriteLine(flower);
    }
  }
}

输出:

List Before Removal:
Rose
Jasmine
Lili
Hibiscus
Daisy

List After Removal:
Rose
Jasmine
Lili

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 C# 中发出 HTTP POST Web 请求

发布时间:2024/02/04 浏览次数:131 分类:编程语言

在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求

在 C# 中调整图像大小

发布时间:2024/02/04 浏览次数:203 分类:编程语言

有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保

在 C# 中下载图片

发布时间:2024/02/04 浏览次数:138 分类:编程语言

有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据

在 C# 中使用秒表

发布时间:2024/02/04 浏览次数:139 分类:编程语言

我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。

在 C# 中获取可执行路径

发布时间:2024/02/04 浏览次数:200 分类:编程语言

有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便