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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:59:30+00:00 2026-05-11T11:59:30+00:00

Sorry to ask this yet again. I’ve been reading the related topics here for

  • 0

Sorry to ask this yet again. I’ve been reading the related topics here for hours and still don’t quite get some of the issues with casting and generics. I’m trying to create our own HashSet class, because we can’t use .net 3.5 yet.

The error is in the RetainAll method, below. It compiles fine but I get a runtime error ‘Unable to cast object of type ‘System.Collections.DictionaryEntry’ to type ‘mycompany.UnivID’ on the foreach statement.

Does this mean I can’t cast a generic object to its implementing type? I’ve tried a couple things like

foreach ((T) obj in set) 

but they don’t compile.

Relevant code is:

public sealed class HashSet<T> : ISet<T> {    private readonly Hashtable set;    public HashSet() {     this.set = new Hashtable();   }    public void RetainAll(ISet<T> c) {     lock (set) {       foreach (T obj in set) {         if (!c.Contains(obj))           this.set.Remove(obj);       }     }   } } 

The implementing class snippet is

public sealed class UnivIDSet : ISet {   private readonly ISet<UnivID> univIDs;    public UnivIDSet() : base() {     univIDs = new HashSet<UnivID>();   } } 

and then we’ve got our UnivID class which are actually the objects in the sets.

  • 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. 2026-05-11T11:59:30+00:00Added an answer on May 11, 2026 at 11:59 am

    I think

    foreach (T obj in set) { 

    should be

    foreach (T obj in set.Values) { 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry to ask this again, I've looked over all the related questions here,
Sorry, this's my first time to ask a question here. So, I don't have
Sorry to have to ask yet another position:fixed related question, but reading through various
Sorry to ask this, but with previouse reading, re-reading of doc and all the
Sorry to ask this question again but I tried several solutions on stackoverflow and
I'm sorry to ask this question but I have spent hours trying to understand
Sorry, not clear how to ask this question. Here's my scenario: Controller HostSite hostSite=new
I'm really sorry to have to ask this, but I clearly don't understand something
I'm sorry to ask this here, as I'm sure the solution is fairly easy
I am sorry to ask this question when it has already been asked but

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.