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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:47:08+00:00 2026-05-11T03:47:08+00:00

I really hate sometimes how IDictionary<TKey, TValue> [key] will throw an exception if the

  • 0

I really hate sometimes how IDictionary<TKey, TValue> [key] will throw an exception if the key doesn’t exist in the dictionary.

Of course there is TryGetValue(), but that seems to have been optimized for performance and not usability.

So I thought, oh I’ll just make an extension method for it – which I did :

public static class CollectionExtensions {     public static TType GetValueOrDefault<TKeyType, TValue, TType>(this IDictionary<TKeyType, TType> dictionary, TKeyType key)     {         TType value = default(TType);          // attempt to get the value of the key from the dictionary         // if the key doesn't exist just return null         if (dictionary.TryGetValue(key, out value))         {             return value;         }         else         {             return default(TType);         }     }     } 

This works fine EXCEPT I cannot seem to get type inference working.

Obviously I want to be able to do the following :

var extraDataLookup = new Dictionary<string, string>(); extraDataLookup['zipcode'] = model.Zipcode;  

and then be able to access the value :

var zipcode = extraDataLookup.GetValueOrDefault('zipcode'); var foo = extraDataLookup.GetValueOrDefault('foo'); // should be null 

I’ve looked at a few things about type inference, inlucing Jon Skeet’s article and even sourcecode to System.Linq.Enumerable in reflector but seem to be missing something.

This works :

extraDataLookup.GetValueOrDefault<string, string,string> ('foo')  

but this doesn’t

extraDataLookup.GetValueOrDefault ('foo')  

What should I be doing.

PS. I’m only looking for solutions to the generic type inference problem, not any other suggestions. Thanks.

  • 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. 2026-05-11T03:47:08+00:00Added an answer on May 11, 2026 at 3:47 am

    You seem to be defining your extension method with three generic types when you only need two. ‘TValue’ and ‘TType’ mean the same thing, don’t they? Try this:

    public static TValue GetValueOrDefault<TKey, TValue>(     this IDictionary<TKey, TValue> dictionary, TKey key) {     TValue value;     // attempt to get the value of the key from the dictionary     dictionary.TryGetValue(key, out value);     return value; }     
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 118k
  • 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 class Program { static void Main(string[] args) { Timer timer… May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Use GROUP_CONCAT SELECT GROUP_CONCAT(bar) FROM TABLE GROUP BY foo; May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Your code doesn't work because the function is not returning… May 11, 2026 at 10:50 pm

Related Questions

I have a self upgrade process implemented where my main application exe launches an
Our service tends to fall asleep during the nights on our client's server, and
In the past I've never been a fan of using triggers on database tables.
As I do my coding I sometimes wonder if I'm doing things the best

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.