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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:49:47+00:00 2026-06-12T18:49:47+00:00

If it exists, what is the C# equivalent of the following Java code: new

  • 0

If it exists, what is the C# equivalent of the following Java code:

new HashMap<Class<? extends BaseClass>, Integer>();

I currently use new Dictionary<Type, int>(), which is more like
new HashMap<Class<?>, Integer>() which is obviously not the same.

(Ignore the differences between HashMap and Dictionary)

Edit: To clarify, I am not trying to define a new class, simply create an instance of HashMap/Dictionary.

  • 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-12T18:49:48+00:00Added an answer on June 12, 2026 at 6:49 pm

    There is no equivalent of the Java wildcard in C#. In Java, the type for types is Class<T> where T is the class itself. The equivalent in C# is the type Type, which is not generic. So it seems that the best you can do is to have, as you said, a Dictionary<Type, int>, and if it’s encapsulated in a class you can restrict what you put in the dictionary in the code (so it will just be a runtime check):

    private Dictionary<Type, int> myDictionary = new Dictionary<Type, int>();
    public void Add(Type type, int number) {
       if (!typeof(BaseClass).IsAssignableFrom(type)) throw new Exception();
       myDictionary.Add(type, number);
    }
    

    You can even implement your own IDictionary with that logic.

    UPDATE

    Another runtime trick I can think of is to use a wrapper class for your types:

    public class TypeWrapper<T>
    {
        public Type Type { get; private set; }
        public TypeWrapper(Type t)
        {
            if (!typeof(T).IsAssignableFrom(t)) throw new Exception();
            Type = t;
        }
        public static implicit operator TypeWrapper<T>(Type t) {
            return new TypeWrapper<T>(t);
        }
    }
    

    (Also implement Equals and GetHashCode, just delegate to Type.)

    And then your dictionary becomes:

    var d = new Dictionary<TypeWrapper<BaseClass>, int>();
    d.Add(typeof(BaseClass), 2);
    d.Add(typeof(Child), 3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the equivalent C++/CLI syntax to the following C# declaration (if such exists):
The following code is hopefully a correct way to return an image that exists
I have the equivalent of the following html: <ul> <a class=iframe href=/posting/form?id=8><li>Publish</li></a> </ul> and
What is the linq equivalent of the following statement ? IF NOT EXISTS(SELECT UserName
What is the equivalent Linq to XML for the following code: public List<listing> GetList()
I am running a test on my new Django code. I have the following
I was wondering if any of the following exist in python: A: non-regex equivalent
Exist a .net class equivalent to the TTimer delphi component? TTimer is used to
I'm looking for the C# equivalent of Java's final . Does it exist? Does
Exists a way to call .net assemblies more specific .dll files in java? I

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.