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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:29:34+00:00 2026-06-14T10:29:34+00:00

I have a function that currently uses a Func<T1, T2> as argument. I want

  • 0

I have a function that currently uses a Func<T1, T2> as argument.
I want it to use an Expression<Func<T1, T2>> as argument so that I can traverse the expression tree in certain circumstances.

For every call I want to use the function, so I would need to Compile() the expression.
In order to not having to recompile all of the expressions, I would like to put them in a dictionary.

The calls to my method looks like this:

var foo = MyFunc(x => x.Field);

Is the following solution correct with regards to the usage of Expression as a dictionary key?

static Dictionary<Expression<Func<T1, T2>>, Func<T1, T2>> s_functions = new Dictionary<Expression<Func<T1, T2>>, Func<T1, T2>>();

public T2 MyFunc(Expression<Func<T1, T2>> selectorExpression)
{
    if (!s_functions.ContainsKey(selectorExpression))
    {
        s_functions.Add(selectorExpression, selectorExpression.Compile());
    }

    Func<T1, T2> selector = s_functions[selectorExpression];
}

EDIT

What would be a good way to solve it from a performance perspective?

  • 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-14T10:29:35+00:00Added an answer on June 14, 2026 at 10:29 am

    Unfortunately not, but the problem will only manifest as a performance bug.

    The issue is that Expression<TDelegate> does not implement IEquatable<Expression<TDelegate>> so the default equality comparer used by the dictionary will only do reference equality. This means that different instances of the same expression will not compare identical, which will lead to cache misses that should have been hits. On the other hand instances of different expressions will always compare different, so there’s no logic bug.

    You can fix this problem by providing a custom equality comparer for the dictionary, but there’s also the small matter of having to write one — it’s going to be a tall order.

    On the bright side, it might be possible to write a comparer that has the same performance bug as the original implementation (but reduced in scope) in exchange for convenience — the reduction in scope might account for all or most the cases you are actually going to encounter! For example, you might want to try comparing based on equality of ToString() representations. As long as the comparer does not produce any false positives there is only room for improvement.

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

Sidebar

Related Questions

I currently have a Javascript function that uses a string to reference an object
I have a decorator that currently requires each function that uses it to have
I currently have a function that will create X tasks of a function based
I currently have a function that grabs the browser windows uri, parses out the
I currently have a function written called saveWorkout that saves an NSMutableArray to another
I have a php script that sends an email. It currently uses the php
I have a PrintDocument and its PrintPage event tied to a function that uses
I have a complex function (optimisation) that can potentially enter in a loop or
I have some code that currently uses a ST monad for evaluation. I like
I currently have a class that uses template arguments. I need an array of

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.