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 6892699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:34:04+00:00 2026-05-27T06:34:04+00:00

i am using a HashSet in order to avoid having two (or more)items with

  • 0

i am using a HashSet in order to avoid having two (or more)items with the same value inside my collection , on my work i need to iterate over my hashset and remove its values but unfortunatly i cant do so , what i am trying to do is:

string newValue = "";
HashSet<string> myHashSet;
myHashSet = GetAllValues(); // lets say there is a function which fill the hashset
foreach (string s in myHashSet)
{
       newValue = func(s) // lets say that func on some cases returns s as  it was and
       if(s != newValue)  // for some cases returns another va
       {
             myHashSet.Remove(s);
             myHashSet.Add(newValue);
       }

}

thanks in advance for your kind help

  • 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-27T06:34:04+00:00Added an answer on May 27, 2026 at 6:34 am

    You cannot modify the container while it’s being iterated. The solution would be to project the initial set into a “modified” set using LINQ (Enumerable.Select), and create a new HashSet from the results of the projection.

    Since if there is a func with the appropriate signature you can directly stick it into the Enumerable.Select method, and since HashSet has a constructor that accepts an IEnumerable<T>, it all comes down to one line:

    var modifiedHashSet = new HashSet(myHashSet.Select(func));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does the HashSet collection introduced in .NET 3.5 preserve insertion order when iterated using
I'm using a HashSet<T> to store a collection of objects. These objects already have
I've got a whitelist of URLs I'm using, inside a HashSet<string> . I'm trying
Can anyone tell me how is ObjectIDGenerator better (worse?) then using HashSet when traversing
I am using a HashSet in my WCF interface [ServiceContract] public interface IwcfServerSync {
I have two objects a user and a role which I make persistant using
I have a HashSet<string> that is instantiated using StringComparer.CurrentCultureIgnoreCase and am making extensive use
I've mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship
I look for a way to observe a collection for changes, without using indices.
I have a collection of items called RegisteredItems. I do not care about the

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.