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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:46:58+00:00 2026-06-04T19:46:58+00:00

The generic dictionary is as follows: public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue> And

  • 0

The generic dictionary is as follows:

public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>

And specific dictionaries can be as follows:

var container = new ConcurrentDictionary<string, Unit>();
var container = new ConcurrentDictionary<string, CustomUnitClass>();

These special dictionaries (with different parameters) have been added in Application state:

HttpContext.Current.Application[key] = container;

When I get the items from Application state (some people here helped me about that; Thanks them), I’m able to check if type is of ConcurrentDictionary in this way:

object d = HttpContext.Current.Application[i];
if (d.GetType().GetGenericTypeDefinition() == typeof(ConcurrentDictionary<,>))

And the last point was left – how to cast object d to generic ConcurrentDictionary:

ConcurrentDictionary<?, ?> unit = d as ConcurrentDictionary<?, ?>;

I don’t want to use specific cast as follows:

ConcurrentDictionary<string, Unit> unit = d as ConcurrentDictionary<string, Unit>;

because the second parameter can be of another type.

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-06-04T19:47:00+00:00Added an answer on June 4, 2026 at 7:47 pm

    I think you might be looking at generics in a slightly wrong way, but that’s okay, lets talk!

    Your IDictionary<TKey, TValue> is perfect, and you’re using it correctly, however I think when it comes to casting back, unless you explicitly know what the type is you’re expecting, there’s no real point in casting it.

    Alternatively what I would recommend, for the purpose of strongly-typed loveliness; you mentioned that potentially the second type, aka the TValue will vary… this is a perfect time to use an interface! Let me demonstrate.

    Our interface

    public interface IModeOfTransport
    {
        string Name { get; set; }
        string Colour { get; set; }
        bool CanFloat { get; set; }
        bool CanFly { get; set; }
        int NumberOfPassengers { get; set; }
    }
    

    Our objects

    public class Car : IModeOfTransport
    {
        // ...
    }
    public class Plane : IModeOfTransport
    {
        // ...
    }
    public class Boat : IModeOfTransport
    {
        // ...
    }
    

    Our implementation

    var modesOfTransport = new Dictionary<string, IModeOfTransport>();
    modesOfTransport.Add("First", new Car());
    modesOfTransport.Add("First", new Plane());
    modesOfTransport.Add("First", new Boat());
    

    Let’s take a break

    As you can from the above, we have a dictionary with a key type of string and a value type of IModeOfTransport. This allows us explicit strongly-typed access to all of the properties and methods of the IModeOfTransport interface. This is recommended if you need to access specific information on a generic value within a dictionary, and you don’t know what the actual object type cast is. By using interfaces it allows us to find similarities.

    Almost done

    object dictionary = HttpContext.Current.Application[i];
    if (dictionary.GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>))
    {
        var modesOfTransport = dictionary as Dictionary<string, IModeOfTransport>;
        foreach (var keyValuePair in modesOfTransport)
        {
            // ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The generic dictionary is as follows: public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue> And
While browsing the implementation of the generic Dictionary<TKey, TValue> class in mscorlib.dll, I noticed
Can anyone please tell me the difference between DictionaryBase and generic Dictionary<TKey, TValue> ,
I have a class that inherits from a generic dictionary as follows: Class myClass
Is it possible in C# to create a System.Collections.Generic.Dictionary<TKey, TValue> where TKey is unconditioned
I get the error Using the generic type 'System.Collections.Generic.Dictionary< TKey, TValue>' requires 2 type
I need a fast replacement for the System.Collections.Generic.Dictionary<TKey, TValue> . My application should be
I have a generic method in a class as follows private static Dictionary<Type, Feed>
I have a class that inherits a generic dictionary and an inteface public class
Looking at System.Collections.Generic.Dictionary<TKey, TValue> , it clearly implements ICollection<KeyValuePair<TKey, TValue>> , but doesn't have

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.