<i id='ywXJV'><tr id='ywXJV'><dt id='ywXJV'><q id='ywXJV'><span id='ywXJV'><b id='ywXJV'><form id='ywXJV'><ins id='ywXJV'></ins><ul id='ywXJV'></ul><sub id='ywXJV'></sub></form><legend id='ywXJV'></legend><bdo id='ywXJV'><pre id='ywXJV'><center id='ywXJV'></center></pre></bdo></b><th id='ywXJV'></th></span></q></dt></tr></i><div id='ywXJV'><tfoot id='ywXJV'></tfoot><dl id='ywXJV'><fieldset id='ywXJV'></fieldset></dl></div>

<small id='ywXJV'></small><noframes id='ywXJV'>

  1. <legend id='ywXJV'><style id='ywXJV'><dir id='ywXJV'><q id='ywXJV'></q></dir></style></legend>

        <bdo id='ywXJV'></bdo><ul id='ywXJV'></ul>
      <tfoot id='ywXJV'></tfoot>

      从列表中删除重复项&lt;T&gt;在 C# 中

      时间:2023-08-27
        <bdo id='oNjeE'></bdo><ul id='oNjeE'></ul>
        <i id='oNjeE'><tr id='oNjeE'><dt id='oNjeE'><q id='oNjeE'><span id='oNjeE'><b id='oNjeE'><form id='oNjeE'><ins id='oNjeE'></ins><ul id='oNjeE'></ul><sub id='oNjeE'></sub></form><legend id='oNjeE'></legend><bdo id='oNjeE'><pre id='oNjeE'><center id='oNjeE'></center></pre></bdo></b><th id='oNjeE'></th></span></q></dt></tr></i><div id='oNjeE'><tfoot id='oNjeE'></tfoot><dl id='oNjeE'><fieldset id='oNjeE'></fieldset></dl></div>

          <small id='oNjeE'></small><noframes id='oNjeE'>

            • <tfoot id='oNjeE'></tfoot>
                <tbody id='oNjeE'></tbody>
              • <legend id='oNjeE'><style id='oNjeE'><dir id='oNjeE'><q id='oNjeE'></q></dir></style></legend>

                本文介绍了从列表中删除重复项&lt;T&gt;在 C# 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                任何人有在 C# 中对通用列表进行重复数据删除的快速方法吗?

                Anyone have a quick method for de-duplicating a generic List in C#?

                推荐答案

                也许你应该考虑使用 HashSet.

                Perhaps you should consider using a HashSet.

                来自 MSDN 链接:

                From the MSDN link:

                using System;
                using System.Collections.Generic;
                
                class Program
                {
                    static void Main()
                    {
                        HashSet<int> evenNumbers = new HashSet<int>();
                        HashSet<int> oddNumbers = new HashSet<int>();
                
                        for (int i = 0; i < 5; i++)
                        {
                            // Populate numbers with just even numbers.
                            evenNumbers.Add(i * 2);
                
                            // Populate oddNumbers with just odd numbers.
                            oddNumbers.Add((i * 2) + 1);
                        }
                
                        Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
                        DisplaySet(evenNumbers);
                
                        Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count);
                        DisplaySet(oddNumbers);
                
                        // Create a new HashSet populated with even numbers.
                        HashSet<int> numbers = new HashSet<int>(evenNumbers);
                        Console.WriteLine("numbers UnionWith oddNumbers...");
                        numbers.UnionWith(oddNumbers);
                
                        Console.Write("numbers contains {0} elements: ", numbers.Count);
                        DisplaySet(numbers);
                    }
                
                    private static void DisplaySet(HashSet<int> set)
                    {
                        Console.Write("{");
                        foreach (int i in set)
                        {
                            Console.Write(" {0}", i);
                        }
                        Console.WriteLine(" }");
                    }
                }
                
                /* This example produces output similar to the following:
                 * evenNumbers contains 5 elements: { 0 2 4 6 8 }
                 * oddNumbers contains 5 elements: { 1 3 5 7 9 }
                 * numbers UnionWith oddNumbers...
                 * numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }
                 */
                

                这篇关于从列表中删除重复项&lt;T&gt;在 C# 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:将 Azure 管道与 Gitlab 集成(持续集成) 下一篇:如何合并2个List&lt;T&gt;并在 C# 中从中删除重

                相关文章

                最新文章

              • <legend id='MPwhp'><style id='MPwhp'><dir id='MPwhp'><q id='MPwhp'></q></dir></style></legend>
                  <i id='MPwhp'><tr id='MPwhp'><dt id='MPwhp'><q id='MPwhp'><span id='MPwhp'><b id='MPwhp'><form id='MPwhp'><ins id='MPwhp'></ins><ul id='MPwhp'></ul><sub id='MPwhp'></sub></form><legend id='MPwhp'></legend><bdo id='MPwhp'><pre id='MPwhp'><center id='MPwhp'></center></pre></bdo></b><th id='MPwhp'></th></span></q></dt></tr></i><div id='MPwhp'><tfoot id='MPwhp'></tfoot><dl id='MPwhp'><fieldset id='MPwhp'></fieldset></dl></div>
                1. <small id='MPwhp'></small><noframes id='MPwhp'>

                    <bdo id='MPwhp'></bdo><ul id='MPwhp'></ul>

                    <tfoot id='MPwhp'></tfoot>