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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:34:44+00:00 2026-05-13T13:34:44+00:00

I am trying to create an overloaded Add method as an extension to the

  • 0

I am trying to create an overloaded Add method as an extension to the OrderedDictionary class and would like to add the key/value based on some curried predicate.

The calling code would look like this:


OrderedDictionary dict = new OrderedDictionary();

Predicate<int> lessThan5 = i=>; i < 5;
Predicate<string> lenOf2 = s=> s.length == 2;

dict.Add("01","Name", lessThan5 );
dict.Add("02","place", lenOf2);

I have created an extension method like so:


public static class CollectionExtensions
{
    public static void Add(this OrderedDictionary s, string k, string v, Predicate p)
    {
        if (p)
        {
            d.Add(k, v);
        }
    }
}

But it doesn’t work because I get a compiler error reading “cannot convert Predicate to bool”.

Does anyone know what I am missing?

Thanks for any help.
-Keith

  • 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-13T13:34:45+00:00Added an answer on May 13, 2026 at 1:34 pm

    The issue is that you are not evaluating your predicate to check and see whether or not the predicate is satisfied. Now, it’s not clear from your question if you want the predicate to test the key or the value. The following checks the key. You should also consider having the method return bool indicating success or failure as I’ve done here.

    static class OrderedDictionaryExtensions {
        public static bool Add(
            this OrderedDictionary dictionary,
            string key,
            string value,
            Predicate<string> predicate
        ) {
            if (dictionary == null) {
                throw new ArgumentNullException("dictionary");
            }
            if (predicate == null) {
                throw new ArgumentNullException("predicate");
            }
            if (predicate(key)) {
                dictionary.Add(key, value);
                return true;
            }
            return false;
        }
    }
    

    Usage;

    // dictionary is OrderedDictionary
    dictionary.Add("key", "value", s => s.Length < 5);
    

    You can actually generalize this a bit since OrderedDictionary is not strongly-typed.

    static class OrderedDictionaryExtensions {
        public static bool Add<TKey, TValue>(
            this OrderedDictionary dictionary,
            TKey key,
            TValue value,
            Predicate<TKey> predicate
        ) {
            if (dictionary == null) {
                throw new ArgumentNullException("dictionary");
            }
            if (predicate == null) {
                throw new ArgumentNullException("predicate");
            }
            if (predicate(key)) {
                dictionary.Add(key, value);
                return true;
            }
            return false;
        }
    }
    

    Usage:

    // dictionary is OrderedDictionary
    dictionary.Add(17, "Hello, world!", i => i % 2 != 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a sparse vector class in C++, like so: template<typename V,
I am trying to create a value template class, where additional properties can be
Trying to create my first iPhone app that would play back audio. When I
Trying to create a list to return some JSON data to a view. Following
I am trying to create a generic list class for use with tiOPF (an
I'm trying to create an iterator class as a member-class for a list class,
I have a vector class with a properly overloaded Vect*float operator and am trying
Trying to create an app that does some socket communication (Writing only). I can
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation

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.