
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Create HashSet from another collection in C#
To create HashSet from another Collection, the code is as follows−
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<int> set1 = new HashSet<int>(); set1.Add(100); set1.Add(200); set1.Add(300); Console.WriteLine("HashSet created from another collection..."); foreach(int i in set1){ Console.WriteLine(i); } HashSet<int> set2 = new HashSet<int>(set1); Console.WriteLine("HashSet created from another collection..."); foreach(int i in set2){ Console.WriteLine(i); } } }
Output
This will produce the following output −
HashSet created from another collection... 100 200 300 HashSet created from another collection... 100 200 300
Example
Let us now see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<string> set1 = new HashSet<string>(); set1.Add("Jacob"); set1.Add("Tom"); set1.Add("Harry"); set1.Add("Harry"); set1.Add("Tom"); set1.Add("Harry"); Console.WriteLine("HashSet created from another collection..."); foreach(string i in set1){ Console.WriteLine(i); } HashSet<string> set2 = new HashSet<string>(set1); Console.WriteLine("HashSet created from another collection..."); foreach(string i in set2){ Console.WriteLine(i); } } }
Output
This will produce the following output −
HashSet created from another collection... Jacob Tom Harry HashSet created from another collection... Jacob Tom Harry
- Related Articles
- Check if a Java HashSet Collection contains another Collection
- Create Octet Tuple from another collection in Java
- Create Ennead Tuple from another collection in Java
- Create Decade Tuple from another collection in Java
- Create LabelValue Tuple from another collection in Java
- Create KeyValue Tuple from another collection in Java
- Create Pair Tuple from another collection in Java
- Create Quintet Tuple from another collection in Java
- Create Triplet Tuple from another collection in Java
- Create Quartet Tuple from another collection in Java
- Create Unit Tuple from another collection in Java
- Create Septet Tuple from another collection in Java
- Create a Queue from another collection in C#?
- Create a new ArrayList from another collection in Java
- Remove all elements in a collection from a HashSet in C#

Advertisements