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

  • Home
  • SEARCH
  • 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 9133731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:32:18+00:00 2026-06-17T08:32:18+00:00

I have this code (which I expect to work but it fails).. I really

  • 0

I have this code (which I expect to work but it fails).. I really have no idea why. Please help

     static void Main(string[] args)
    {
        var x = new MyKeyedCollection();

        x.Add(new MyType() { Key = 400L, Value = 0.1 });
        x.Add(new MyType() { Key = 200L, Value = 0.1 });
        x.Add(new MyType() { Key = 100L, Value = 0.1 });
        x.Add(new MyType() { Key = 300L, Value = 0.1 });

        //foreach (var item in x)
        for (int i = 0; i < x.Count; i++)
        {
            //Debug.WriteLine(item.PriceLevel);
            Debug.WriteLine(x[i].Key);
        }
    }
}

public class MyType 
{
    public long Key;
    public double Value;
}

public class MyKeyedCollection : KeyedCollection<long, MyType>
{
    protected override long GetKeyForItem(MyType item)
    {
        return item.Key;
    }
}

Exception:

System.Collections.Generic.KeyNotFoundException was unhandled
Message=The given key was not present in the dictionary.
Source=mscorlib StackTrace:
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary2.get_Item(TKey key)
at System.Collections.ObjectModel.KeyedCollection
2.get_Item(TKey
key)

at KeyedCollectionTest.Program.Main(String[] args) in …\Program.cs:line 25
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart() InnerException:

How come it tries to get Key instead of index? Key is clearly long and not int. I am sure I used KeyedCollection before and it worked just fine for long key and int index.

I tried to compile in ver 2, 3.5, 4, 4.5 (using VS2012)…

Dont get it.

  • 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-06-17T08:32:19+00:00Added an answer on June 17, 2026 at 8:32 am

    How come it tries to get Key instead of index? Key is clearly long and not int.

    But int is convertible to long, so it’s a valid candidate member.

    The problem is that the this[TKey key] indexer is originally declared in KeyedCollection, whereas the this[int index] indexer is originally declared in Collection. The rules of overload resolution specify that the most derived class is searched first, and only the members which are first declared in that type are considered to start with. Only if that search fails does the compiler move up to the next level in the type hierarchy.

    So if you write:

    Collection<MyType> collection = x;
    for (int i = 0; i < x.Count; i++)
    {
        Debug.WriteLine(collection[i].Key);
    }
    

    it will work – because the compile-time type of collection is just Collection<T>, which only has the “int index” indexer.

    Here’s an example which shows the same behaviour without using generics, an indexer, or abstract classes:

    using System;
    
    class Base
    {
        public void Foo(int x)
        {
            Console.WriteLine("Base.Foo(int)");
        }
    }
    
    class Derived : Base
    {
        public void Foo(long y)
        {
            Console.WriteLine("Derived.Foo(long)");
        }
    }
    
    class Program
    {
        static void Main()
        {
            Derived x = new Derived();
            Base y = x;
            x.Foo(5); // Derived.Foo(long)
            y.Foo(5); // Base.Foo(int)
        }
    }
    

    See my article on overloading for some more of the interesting rules involved.

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

Sidebar

Related Questions

I have this code which does the trick: #include <stdio.h> int main() { int
I have this code which gets WP posts, but it gets all the posts
I have the following jQuery code which does not work as I expect it.
I have tried stuff like =~ \[[A-Za-z0-9]+\] which I would expect would work but
I have this code which adds a x number of views to a Multiview
I have this code which is called at an onChange event of an function
I have this code (which is way simplified from the real code): public interface
I have this code which compiles and works as expected: class Right {}; class
I have this code which will include template.php file from inside each of these
I have this code which should create a splash image with either no animation

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.