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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:20:40+00:00 2026-06-15T04:20:40+00:00

I am trying to setup custom IDictionary that will allow Object as my TValue.

  • 0

I am trying to setup custom IDictionary that will allow Object as my TValue.

Here is what it looks like:

public class NullTolerantDictionary<TKey, TValue> 
             : Dictionary<TKey, TValue> where TValue : class
{
    public TValue this[TKey key]
    {
        get
        {
            TValue value;
            if (TryGetValue(key, out value))
            {
                return value;
            }
            else
            {
                return DependencyProperty.UnsetValue;
            }
        }        
    }
}

When this compiles it says:

Cannot implicitly convert type ‘object’ to ‘TValue’.

So to fix this I change my type constraint to be like this:

where TValue : object

(object instead of class)

I then get this message:

Constraint cannot be special class ‘object’

How do I get around this?

Extra credit for explaining why object is not able to be a constraint.

  • 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-15T04:20:41+00:00Added an answer on June 15, 2026 at 4:20 am

    The problem here is not with generic type argument constraints but with the signature of the indexer.

    The indexer is documented to always return a TValue, whatever that happens to be. The else branch however tries to return DependencyProperty.UnsetValue, which is of type object. Since not all objects are TValues, the compiler complaints (“no implicit conversion”).

    It would make no difference if you placed any number of constraints on TValue, and even if you could constrain it to object. The type argument could still be set to anything more derived than object and DependencyProperty.UnsetValue would still not be convertible to that type.

    If you want to achieve such an effect you could use a public static read-only property such as

    public class NullTolerantDictionary<TKey, TValue> 
             : Dictionary<TKey, TValue> where TValue : class
    {
        public static TValue MissingValue { get; private set; }
    }
    

    and return that from the indexer, presumably so that it can then be used in reference comparisons.

    But that would still leave open the question: how do actually get a TValue value to use as MissingValue? You could place the new() constraint on TValue and create an instance inside a static constructor, but that would decrease the possible applications for this class. This results in the design starting to get a bit clunky to use, and since that’s exactly what you are trying to avoid there’s not much point in going that way.

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

Sidebar

Related Questions

I'm trying to build an application that will allow the user to setup N
I am trying to create a custom dialog class that inherits from Dialog that
I'm trying to create a custom timer that will record cumulative time passage separated
I'm trying to setup custom validation for a checkbox. I have 7 checkboxes each
I am trying to setup a custom attribute in C# to set whether a
I'm using nopCommerce 2.3 and trying to setup a custom Area but the area
Im trying to setup my asp.net mvc page like the following image: The header
I'm fairly new to CakePHP and am trying to setup a custom admin area
I'm trying to setup a player using mediaelement.js. I'm implementing my own custom playlist
I'm trying to setup a custom site-package directory (Python 2.6 on Windows Vista). For

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.