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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:42:54+00:00 2026-05-11T16:42:54+00:00

I need to create a Dictionary object as a member field key = string

  • 0

I need to create a Dictionary object as a member field
key = string
value = an instance of Action<T> where T could be different per entry, e.g. long, int, string (a ValueType or a RefType)

However can’t get the compiler to agree with me here.. To create a member field it seems to need a fixed T specification. I’ve passed my limit of struggle time before awareness that ‘It shouldn’t be this difficult’

Might be a generics noob mistake. Please enlighten..

The usage would be something like this

m_Map.Add("Key1", 
   new delegate(long l) {Console.Writeline("Woohoo !{0}", l.ToString();));
  • 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-11T16:42:54+00:00Added an answer on May 11, 2026 at 4:42 pm

    You can’t, basically. How would the compiler know what type you were interested in for any particular entry?

    You can’t even explain a relationship of Dictionary<Type, Action<T>> where each dictionary entry has a key which is a type and an action which uses that type. Generics just doesn’t support that relationship.

    If you will know the kind when you try to use it, you can just make it a Dictionary<string, Delegate> and cast the value when you fetch it. Alternatively, you could use Action<object> and live with the boxing and cast you’ll end up with.

    Note that to use anonymous methods or lambda expressions with just Delegate, you’ll need to cast – or write a handy conversion method:

    public static Delegate ConvertAction<T>(Action<T> action)
    {
        return action;
    } 
    

    That way you can write:

    Delegate dlg = ConvertAction((long x) => Console.WriteLine("Got {0}", x));
    

    or in the dictionary context:

    var dict = new Dictionary<string, Delegate>();
    dict["Key1"] = ConvertAction((long x) => Console.WriteLine("Got {0}", x));
    

    You’ll still need to cast to the right type when you fetch the value out of the dictionary again though…

    A different tack…

    Instead of exposing a dictionary directly, you could encapsulate the dictionary in your own type, and have a generic Add method:

    public void Add<T>(string key, Action<T> action)
    

    So it would still be a Dictionary<string, Delegate> behind the scenes, but your type would make sure that it only contained values which were delegates taking a single argument.

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

Sidebar

Ask A Question

Stats

  • Questions 105k
  • Answers 105k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Design 2 and Design 4 are the best ways to… May 11, 2026 at 8:45 pm
  • Editorial Team
    Editorial Team added an answer It could be a character encoding problem. Your JSP header… May 11, 2026 at 8:45 pm
  • Editorial Team
    Editorial Team added an answer With Windows, you can never be sure. However, this(search for… May 11, 2026 at 8:45 pm

Related Questions

I have created a dynamic typing system in C in order to create a
Garbage collection in .NET leads many to believe that lightweight objects can be treated
Edit: I am using SqlDataAdapters to fill the data sets. Sorry--I should have been
I recently asked this question: Expose XML or Objects - thanks all for the
Is there a simple way to retrieve the length of an associative array (implemented

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.