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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:03:49+00:00 2026-05-26T07:03:49+00:00

after reading this very interesting thread on duplicate removal , i ended with this

  • 0

after reading this very interesting thread on duplicate removal, i ended with this =>

    public static IEnumerable<T> deDuplicateCollection<T>(IEnumerable<T> input)
    {
        var hs = new HashSet<T>();
        foreach (T t in input)
            if (hs.Add(t))
                yield return t;
    }        

by the way, as i’m brand new to C# and coming from Python, i’m a bit lost between casting and this kind of thing… i was able to compile and build with :

            foreach (KeyValuePair<long, List<string>> kvp in d)
            {
                d[kvp.Key] = (List<string>) deDuplicateCollection(kvp.Value);
            }

but i must have missed something here… as i get a “System.InvalidCastException” @ runtime, maybe could you point interesting things about casting and where i’m wrong? Thank you in advance.

  • 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-26T07:03:49+00:00Added an answer on May 26, 2026 at 7:03 am

    First, about the usage of the method.

    Drop the cast, invoke ToList() on the result of the method. The result of the method is IEnumerable<string>, this is not a List<string>. The fact the source is originally a List<string> is irrelevant, you don’t return the list, you yield return a sequence.

    d[kvp.Key] = deDuplicateCollection(kvp.Value).ToList();
    

    Second, your deDuplicateCollection method is redundant, Distinct() already exists in the library and performs the same function.

    d[kvp.Key] = kvp.Value.Distinct().ToList();
    

    Just be sure you have a using System.Linq; in the directives so you can use these Distinct() and ToList() extension methods.

    Finally, you’ll notice making this change alone, you run into a new exception when trying to change the dictionary in the loop. You cannot update the collection in a foreach. The simplest way to do what you want is to omit the explicit loop entirely. Consider

    d = d.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Distinct().ToList());
    

    This uses another Linq extension method, ToDictionary(). Note: this creates a new dictionary in memory and updates d to reference it. If you need to preserve the original dictionary as referenced by d, then you would need to approach this another way. A simple option here is to build a dictionary to shadow d, and then update d with it.

    var shadow = new Dictionary<string, string>();
    foreach (var kvp in d)
    { 
        shadow[kvp.Key] = kvp.Value.Distinct().ToList();
    }
    
    foreach (var kvp in shadow)
    {
        d[kvp.Key] = kvp.Value;
    }
    

    These two loops are safe, but you see you need to loop twice to avoid the problem of updating the original collection while enumerating over it while also preserving the original collection in memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After posting this question and reading that one I realized that it is very
After reading this description of late static binding (LSB) I see pretty clearly what
I've been wondering about this for a while after reading through an interesting article
After reading this very informative (albeit somewhat argumentative) question I would like to know
After reading this question , I was reminded of when I was taught Java
After reading this answer: best way to pick a random subset from a collection?
After reading this discussion and this discussion about using CrashRpt to generate a crash
After reading this on the question How do I uniquely identify computers visiting my
After reading this post I kinda felt in the same position as the guy
After reading this article on thedailywtf.com, I'm not sure that I really got 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.