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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:28:10+00:00 2026-05-27T02:28:10+00:00

Suppose I have a class: class MyClass { public int MyMethod(Func<int, int> f) {

  • 0

Suppose I have a class:

class MyClass {
    public int MyMethod(Func<int, int> f) { return 0; }
    public int MyMethod(Expression<Func<int, int>> f) { return 1; }
}

When I try to call the method with a lambda expression, I get a compile error stating that the call is ambiguous between the two overloads:

var myClass = new MyClass();
myClass.MyMethod(x => 1 + x); // Error!

while, of course, calling with an explicit type works fine:

myClass.MyMethod((Func<int, int>)(x => 1 + x)); // OK, returns 0
myClass.MyMethod((Expression<Func<int, int>>)(x => 1 + x)); // OK, returns 1

The expression tree contains more information (the actual code), and I may want to make use of this information when it is available. But I also want my code to work with delegates. Unfortunately, this ambiguity makes it so I have to find another way to distinguish between the two calls, which messes up an otherwise clean API.

The C# spec doesn’t say anything about this specific situation, so in this sense the behavior does match the spec.

However, there is an argument to be made that the expression tree should be preferred over the delegate. The Compile method acts as an explicit conversion from an expression tree to a delegate. The expression tree contains more information, and when you compile to a delegate, you lose that information. There is no conversion in the other direction.

Are there any reasons not to prefer the expression tree?

  • 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-27T02:28:10+00:00Added an answer on May 27, 2026 at 2:28 am

    Answering the question on the title, they are ambiguous because the type system has no concept of “lambda expression”. It’s a compiler feature which can be converted to a delegate or an expression tree so you need to be explicit to which type you want to convert it. Most of the times the target is also inferred automatically by the compiler because of the context in which the lambda expression is used. For example, the use of lambdas in IEnumerable extension methods versus the use of lambdas in IQueryable extension methods.

    Now, to answer the question on why not always prefer the expression tree, you have the performance argument that MagnatLU already stated. If you accept an Expression and then call Compile to be able to execute it, then it will always be slower when compared to accepting a delegate.

    There is also a semantic difference between the two, a delegate is just a way to execute some code while an expression tree is a description of the actual code.

    If I were you I would opt to change the name of the method accepting the expression to something that clearly reflects what it does extra to the delegate based one.

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

Sidebar

Related Questions

Let's suppose I have this object: [Serializable] public class MyClass { public int Age
Suppose I have a class public class MyClass { private Set<String> set = new
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose, i have class, define('property', 'test'); class myClass { public $test; } $obj=new myClass;
Suppose I have the class: class MyClass { protected $protected; private $_private; public function
Suppose I have a class like this: class MyClass { private: vector<MyOtherClass> myMember; public:
Suppose I have simple class like: class MyClass { private $_prop; public function getProp()
Suppose I have the following C# class class MyClass { private int _i; private
suppose you have an big unsorted List<MyClass> of public class MyClass { public Guid
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a

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.