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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:51:36+00:00 2026-05-12T07:51:36+00:00

Can someone explain why the first of the two following examples is valid and

  • 0

Can someone explain why the first of the two following examples is valid and the other is not? More specifically, how is a relationship created between T and TProperty in the first example?

//Example 1  
class SomeClass<T> where T : class
{
    void SomeMethod<TProperty>( Expression<Func<T,TProperty>> expression ){ ... }
}

//Example 2  
class SomeClass
{
    void SomeMethod<T,TProperty>( Expression<Func<T,TProperty>> expression ) 
         where T : class{ ... }
}

Given the two examples I would expect that the following implementations would work, but the second one does not.

//Implementation w/ Example 1  
var sc = new SomeClass<MyDateClass>();
sc.SomeMethod( dt => dt.Year );

//Implementation w/ Example 2
var sc = new SomeClass();
sc.SomeMethod<MyDateClass>( dt => dt.Year );

What I’m having difficulty wrapping my mind around is how the first example/implementation can ignore the TProperty generic type when executing SomeMethod, yet the second example/implementation can’t and how an implicit relationship seems to be established between T and TProperty in example/implementation 1.

Solution
Change signature of method in example 2 as follows:

void SomeMethod<T>( Expression<Func<T,Object>> expression ){ ... }

While this will allow arbitrary objects to be used in the expression as a result it does allow for property articulation as described in implementation 2.

  • 1 1 Answer
  • 2 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-12T07:51:37+00:00Added an answer on May 12, 2026 at 7:51 am

    You’re allowing the compiler to infer the type parameters. This works fine for case 1:

    class SomeClass<T> where T : class {
        void SomeMethod<TProperty>( Expression<Func<T,TProperty>> expression ){ ... }
    }
    

    because you first declare an instance:

    var sc = new SomeClass<MyDateClass>();
    

    which tells the compiler that T is MyDateClass. Then, when you call the method:

    sc.SomeMethod( dt => dt.Year );
    

    the compiler knows that T is MyDateClass, so T.Year must be an int. That’s enough info to resolve SomeMethod as SomeMethod<MyDateClass, int>.

    Your second example, however, is explicitly stating the type parameter(s) – telling the compiler to not infer it:

    sc.SomeMethod<MyDateClass>( dt => dt.Year );
    

    Unfortunately, it is trying to call SomeMethod with only a single type parameter (the <MyDateClass> bit). Since that doesn’t exist, it’ll complain and say you need 2 type parameters.

    If, instead, you were to call it like you did the first example:

    sc.SomeMethod( dt => dt.Year );
    

    The compiler will complain, telling you that it cannot infer the type parameters – there’s simply not enough info to determine what dt is. So, you could explicitly state both type parameters:

    sc.SomeMethod<MyDateClass, int>( dt => dt.Year );
    

    Or, tell the compiler what dt is (which is what you did in the first example by newing SomeClass<MyDateClass>):

    sc.SomeMethod((MyDateClass dt) => dt.Year );
    

    Edits and Updates:

    So effectively the compiler is performing magic in the first example? Making an assumption that TProperty should be an int because .Year is an int? That would answer my question, can’t say it’s satisfying though.

    Not really magic, but a series of small steps. To illustate:

    1. sc is of type SomeClass<MyDateClass>, making T = MyDateClass
    2. SomeMethod is called with a parameter of dt => dt.Year.
    3. dt must be a T (that’s how SomeMethod is defined), which must be a MyDateClass for the instance sc.
    4. dt.Year must be an int, as MyDateClass.Year is declared an int.
    5. Since dt => dt.Year will always return an int, the return of expression must be int. Therefore TProperty = int.
    6. That makes the parameter expression to SomeMethod, Expression<Func<MyDateClass,int>>. Recall that T = MyDateClass (step 1), and TProperty = int (step 5).
    7. Now we’ve figured out all of the type parameters, making SomeMethod<T, TProperty> = SomeMethod<MyDateClass, int>.

    [B]ut what’s the difference between specifying T at the class level and specifying at the method level? SomeClass<SomeType> vs. SomeMethod<SomeType>… in both cases T is known is it not?

    Yes, T is known in both cases. The problem is that SomeMethod<SomeType> calls a method with only a single type parameter. In example 2, there are 2 type parameters. You can either have the compiler infer all type parameters for the method, or none of them. You can’t just pass 1 and have it infer the other (TProperty). So, if you’re going to explicitly state T, then you must also explicitly state TProperty.

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

Sidebar

Related Questions

Can someone explain the difference between these two, the first one is taken from
Can someone please explain the difference between these two examples in the context of
Can someone explain to me why the following works for the first test but
Can someone please explain the following How was the first JDK release unit Tested?
Can someone explain not what MustOverride does, but why use it? Is it to
Im new to C# programming. Can someone please explain the following code: Console.WriteLine( {0}{1,10},
Can someone explain this to me? I have two queries below with their results.
Can someone explain it succinctly? Can it be used with non-Silverlight clients?
Can someone explain to me this odd thing: When in python shell I type
Can someone explain how can I add custom font, css classes styles to Contribute?

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.