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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:41:04+00:00 2026-05-19T02:41:04+00:00

I have defined the following classes and methods: using System; using System.Linq.Expressions; using System.Windows.Forms;

  • 0

I have defined the following classes and methods:

 using System;
 using System.Linq.Expressions;
 using System.Windows.Forms;

 public class ReturnValue<T, S> {}

 public class Something<T>
 {
     // Sorry about the odd formatting. Trying to get it to fit nicely...
     public ReturnValue<T, C>
     Do<C, S>(C control, Expression<Func<C, S>> controlProperty)
     where C : Control
     {
         return new ReturnValue<T, C>();
     }

     public ReturnValue<T, ToolStripItem>
     Do<S>(ToolStripItem control, Expression<Func<ToolStripItem, S>> controlProperty)
     {
         return new ReturnValue<T, ToolStripItem>();
     }
 }

This compiles fine. Woo hoo! Half-way there. Then, I try to use it later with code like this:

 var toolStripItem = new ToolStripStatusLabel();

 var something = new Something<string>();
 something.Do(toolStripItem, t => t.Text); // Does not compile

This, however, dies with the following error message

The type ToolStripStatusLabel cannot be used as type parameter C in the generic type or method Something<T>.Do<C,S>(C, Expression<Func<C,S>>). There is no implicit reference conversion from ToolStripStatusLabel to Control.

It seems to me that the C# compiler has failed in this case though the two methods do not create a set of ambiguous method declarations. Control and ToolStripStatusLabel exist as siblings in the inheritance tree of Component. I would think that the compiler would have enough information to correctly bind the method invocation in the client code.

However, if I do the same thing with my own sibling classes, then everything compiles fine.

 public class Parent {}
 public class Child1 : Parent {}
 public class Child2 : Parent {}

 public class Something2<T>
 {
     public ReturnValue<T, C>
     Do<C, S>(C control, Expression<Func<C, S>> controlProperty)
     where C : Child1
     {
         return new ReturnValue<T, C>();
     }

     public ReturnValue<T, Child2>
     Do<S>(Child2 control, Expression<Func<Child2, S>> controlProperty)
     {
         return new ReturnValue<T, Child2>();
     }
 }

 var child2 = new Child2();
 var something2 = new Something2<string>();
 something2.Do(child2, c => c.GetType()); // Compiles just fine

Can anyone shed light on what I have done wrong, if anything?

  • 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-19T02:41:05+00:00Added an answer on May 19, 2026 at 2:41 am

    The problem is that the first method is in the candidate set for overload resolution, because the type constraint C : Control is only applied after overload resolution has been performed. I believe you’re expecting it to be weeded out early – and it isn’t.

    Now, if you treat C = ToolStripItem, the first overload is more specific than the second – so the result of overload resolution is to pick that first version.

    The type constraint validation is then applied… and fails.

    I have a blog post on this matter which may help you to understand the process, and then another blog post where I apply the rules in a rather silly way.

    EDIT: In your second example, the type of the argument is exactly the type specified in the first parameter, so the first method doesn’t end up being more specific. The second method wins due to having fewer type parameters (I think; I haven’t checked it in detail) and is then validated and passes.

    To put it back into ToolStripItem terms, you could actually make your first sample compile with one simple change:

    // Change this
    var toolStripItem = new ToolStripStatusLabel();
    // To this...
    ToolStripItem toolStripItem = new ToolStripStatusLabel();
    

    Changing the compile-time type of toolStripItem from ToolStripStatusLabel to ToolStripItem takes away the “advantage” that the first method had, so it then compiles.

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

Sidebar

Related Questions

I have the following members defined in a class that I'm trying to deserialise:
I have a Repository Class with the following method... public T Single<T>(Predicate<T> expression) {
I have an abstract class called 'Template' defined as: [DataContract] public abstract class Template
I have the following route defined routes.MapRoute( ItemName, {controller}/{action}/{projectName}/{name}, new { controller = Home,
So I have the following user defined type in my oracle database: CREATE OR
I have created UITableCellView class called NoteCell . The header defines the following: #import
I have defined an interface in C++, i.e. a class containing only pure virtual
I have the following problem: I'm using the wsimport ant task to create a
I have a bunch of repository classes which I want to unit-test using Visual
I have a master-detail relationship between 2 classes. The master class will contain 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.