using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// 创建一个包含重复元素的列表
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
// 使用 Distinct() 方法去重
List<int> uniqueNumbers = numbers.Distinct().ToList();
// 输出去重后的列表
Console.WriteLine("去重后的列表:");
foreach (var num in uniqueNumbers)
{
Console.WriteLine(num);
}
}
}
List<int> numbers:创建一个包含重复元素的整数列表。Distinct():这是 LINQ 提供的一个扩展方法,用于去除集合中的重复元素。ToList():将 IEnumerable<int> 转换为 List<int>,以便我们可以继续使用列表的功能。foreach 循环:遍历去重后的列表并输出每个元素。这段代码展示了如何使用 C# 和 LINQ 来对一个包含重复元素的列表进行去重操作。
上一篇:c# .net
下一篇:c# urlencode
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站