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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:25:50+00:00 2026-06-12T17:25:50+00:00

I have the following method in an external class public static void DoStuffWithAnimals(IDictionary<string, Animal>

  • 0

I have the following method in an external class

public static void DoStuffWithAnimals(IDictionary<string, Animal> animals)

In my calling code, I already have a Dictionary<string, Lion> object, but I can’t pass this in as this method’s argument. So a IDictionary<,> isn’t contravariant? I can’t see any reason why this shouldn’t work.

The only solution I can think of is:

var animals = new Dictionary<string, Animal>();

foreach(var kvp in lions) {
    animals.Add(kvp.Key, kvp.Value);
}

Is there no way to pass this dictionary into this method, without having to create a new dictionary of the same objects?


EDIT:

As it’s my method, I know that the only member I’m using from the dictionary is the getter of TValue this[TKey key], which is a member of IDictionary<TKey, TValue>, so in this scenario, I’m unable to use a ‘wider’ type for the parameter.

  • 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-12T17:25:51+00:00Added an answer on June 12, 2026 at 5:25 pm

    Solution wise you could do something like this, just pass in an accessor instead:

        public static void DoStuffWithAnimals(Func<string, Animal> getAnimal)
        {
        }
    
        var dicLions = new Dictionary<string, Lion>();
        DoStuffWithAnimals(s => dicLions[s]);
    

    Obviously that is likely to be a bit simple for your needs, but if you only need a couple of the dictionary methods it’s pretty easy to get that in place.

    This is another way that gives you a bit of code re-use between your animals:

        public class Accessor<T> : IAnimalAccessor where T : Animal
        {
            private readonly Dictionary<string, T> _dict;
    
            public Accessor(Dictionary<string, T> dict)
            {
                _dict = dict;
            }
    
            public Animal GetItem(String key)
            {
                return _dict[key];
            }
        }
    
        public interface IAnimalAccessor
        {
            Animal GetItem(string key);
        }
    
        public static void DoStuffWithAnimals(IAnimalAccessor getAnimal)
        {
        }
    
        var dicLions = new Dictionary<string, Lion>();
        var accessor = new Accessor<Lion>(dicLions);
        DoStuffWithAnimals(accessor);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
Imagine the following code: class foreach_convert { public static void method2() { List<IComparable> x
I have the following method: public string Phase(string phase) { return Phase 1; }
I have the following utility: class Worker { public void DoWorkAsync(Action callback) { Action
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have the following method that returns void and I need to use it
I have the following method in my UIButton class: - (id)initWithCoder:(NSCoder *)aDecoder{ self =
I have the following method which is called when the value Edit Class... is
I have the following C++ code (simplified version): class Shape { bool isCircle =
I have the following code below to generate an OpenSSL RSA public and private

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.