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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:11:12+00:00 2026-05-23T01:11:12+00:00

Given the following classes: public class Class1<TObject> { protected void MethodA<TType>(Expression<Func<TObject, TType>> property, ref

  • 0

Given the following classes:

public class Class1<TObject> {
    protected void MethodA<TType>(Expression<Func<TObject, TType>> property, ref TType store, TType value) {
    }
}

public class Class2<TObject> : Class1<Class2<TObject>>{
    private int _propertyInt;
    public int PropertyInt {
        get { return _propertyInt; }
        set { MethodA(c2 => c2.PropertyInt, ref _propertyInt, value); }
    }
}

public class Class3 : Class2<Class3> {
    private float _propertyFloat;
    public float PropertyFloat {
        get { return _propertyFloat; }
        set { MethodA(c3 => c3.PropertyFloat, ref _propertyFloat, value); }
    }
}

For Class2 the C# compiler infers the generic type of the base class for the lambda expression in the ‘PropertyInt’ property setter, but for Class3 the compiler infers the base class, not just the generic type of the base class. Why is this? What is the criteria for the inferred type in the code sample. 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. Editorial Team
    Editorial Team
    2026-05-23T01:11:12+00:00Added an answer on May 23, 2026 at 1:11 am

    First off, the TObject generic parameter is defined in Class1. That TObject is used in Class1 as the type argument in MethodA.

    In Class2, the TObject passed to the base (Class1) is a Class2, and so the lambda can infer the local property _propertyInt.

    In Class3, the TObject passed to the base is a Class2, not a Class3. Therefore, the lambda’s argument is inferred, but it is inferred as a Class2, not a Class3.

    The fact that Class2 also has a type parameter named TObject is entirely coincidental- I think you are expecting that anything passed to that TObject will be transitively passed on to Class1, which it is not.

    If you defined Class3 as follows, it would work:

    public class Class3 : Class1<Class3> { ... }
    

    Given the comment, then might I offer this extension method based solution, (assuming that the type parameters were only for the purpose of making this work):

    public class Class1
    {
    }
    
    public static class StaticClass1
    {
        public static void MethodA<TZen, TType>(this TZen zen, Expression<Func<TZen, TType>> property, ref TType store, TType value) where TZen : Class1
        {
            // Do whatever here...
        }
    }
    
    public class Class2 : Class1
    {
        private int _propertyInt;
        public int PropertyInt
        {
            get { return _propertyInt; }
            set { this.MethodA(c2 => c2.PropertyInt, ref _propertyInt, value); }
        }
    }
    
    public class Class3 : Class2
    {
        private float _propertyFloat;
        public float PropertyFloat
        {
            get { return _propertyFloat; }
            set { this.MethodA(c3 => c3.PropertyFloat, ref _propertyFloat, value); }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following two classes: public class ABC { public void Accept(Ordering<User> xyz) {
Given the following POCO classes: public class Certification { public int Id { get;
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
Given the following classes: public class Container { private List<SomeData> list; // public getter
Given the following: public abstract class Base { // other stuff public static void
Given the following classes and controller action method: public School { public Int32 ID
Given the following domain classes: class Post { SortedSet tags static hasMany = [tags:
Given the following interfaces/classes: public interface IRequest<TResponse> { } public interface IHandler<TRequest, TResponse> where
I have the following classes. public class B { public A a; public B()
assume the following class is given: class Base{ public: Base() {} Base( const Base&

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.