Home > Backend Development > C#.Net Tutorial > Collections in C#

Collections in C#

PHPz
Release: 2023-09-05 15:37:02
forward
1529 people have browsed it

C# 中的集合

#A collection in C# is a HashSet. HashSet in C# eliminates duplicate strings or elements in an array. In C#, it is an optimized set collection

Declare HashSet -

var h = new HashSet<string>(arr1);
Copy after login

Above, we have set the declared array arr1 in the HashSet.

Now set it on the array to remove duplicate words -

string[] arr2 = h.ToArray();
Copy after login

Let us see an example of removing duplicate strings using C# HashSet.

Here, we have repeated elements -

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      string[] arr1 = {"Table","Chair","Pen","Clip","Table"};
      Console.WriteLine(string.Join(",", arr1));

      // HashSet
      var h = new HashSet<string>(arr1);

      // eliminates duplicate words
      string[] arr2 = h.ToArray();
      Console.WriteLine(string.Join(",", arr2));
   }
}
Copy after login

The above is the detailed content of Collections in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template