Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3334912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:57:26+00:00 2026-05-17T23:57:26+00:00

Does the .NET BCL have an immutable Set type? I’m programming in a functional

  • 0

Does the .NET BCL have an immutable Set type? I’m programming in a functional dialect of C# and would like to do something like

new Set.UnionWith(A).UnionWith(B).UnionWith(C)

But the best I can find is HashSet.UnionWith, which would require the following sequence of calls:

HashSet composite = new HashSet();
composite.UnionWith(A);
composite.UnionWith(B);
composite.UnionWith(C);

This use is highly referentially opaque, making it hard to optimize and understand. Is there a better way to do this without writing a custom functional set type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:57:27+00:00Added an answer on May 17, 2026 at 11:57 pm

    Update

    This answer was written some time ago, and since then a set of immutable collections have been introduced in the System.Collections.Immutable namespace.

    Original answer

    You can roll out your own method for this:

    public static class HashSetExtensions {
      public static HashSet<T> Union<T>(this HashSet<T> self, HashSet<T> other) { 
        var set = new HashSet<T>(self); // don't change the original set
        set.UnionWith(other);
        return set;
      }
    }
    

    Use it like this:

    var composite = A.Union(B).Union(C);
    

    You can also use LINQ’s Union, but to get a set, you’ll need to pass the result to the HashSet constructor:

    var composite = new HashSet<string>(A.Union(B).Union(C));
    

    But, HashSet itself is mutable. You could try to use F#’s immutable set.

    Also, as mentioned in the comments by ErikE, using Concat yields the same result and probably performs better:

    var composite = new HashSet<string>(A.Concat(B).Concat(C));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does dot net have an interface like IEnumerable with a count property? I know
Does ASP.NET MVC have anything like the RESTful out-of-the-box features like Ruby on Rails
Does .net have a way to determine whether the local filesystem is case-sensitive?
Does ASP.NET MVC provide a standard validator functionality or do you have to create
Does anybody know of or have a detailed list of how the BCL in
Does .NET framework have a collection of icons for use in Windows Forms or
Does .NET already include something which I can use to prevent selection of a
Possible Duplicate: How does .net managed memory handle value types inside objects? I have
Does .NET come with a class capable of representing extremely large integers, such as
Does .NET Compact Framework 3.5 work on Windows Mobile 2003 SE without limitations?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.