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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:39:53+00:00 2026-05-23T12:39:53+00:00

I am having some issues surrounding the calling of a generic method. In the

  • 0

I am having some issues surrounding the calling of a generic method. In the example below, when I call ServiceCar from the base class I get an error when that method is defined in Dealer as:

Definition 1: ServiceCar<C>(C carToService) where C : Car<C>.

But I do not get an error in the base Class when that method is defined in Dealer as:

Definition 2: ServiceCar<C>(Car<C> carToService) where C : Car<C>

public abstract class Car<T> where T: Car<T>
{
     public bool isServiced;
     public string serviceMessage;

     public virtual void SendToService()
     {
          Dealer.ServiceCar<T>(this);       // error here when Definition 1 used
          serviceMessage = "Your car is clean.";
     }
 }

 public class Ford: Car<Ford>
 {
      public override void SendToService()
      {
           Dealer.ServiceCar<Ford>(this);
           serviceMessage = "Your Ford is clean.";
      }
 }

 public class Dealer
 {
      // When the parameter is defined as C (as commented below) an error occurs
      // When the parameter is defined as Car<C> there are no errors
      // public static void ServiceCar<C>(C carToService) where C : Car<C>
      public static void ServiceCar<C>(Car<C> carToService) where C : Car<C>
      {
           carToService.isServiced = true;
      }
 }

My confusion is that Microsoft says that “where T: means the type argument must be or derive from the specified base class” Well in the case of definition 1 (which does not compile) C is Car<C>. So why isn’t the type constraint parameter helping me out. The error I am getting reads “…cannot convert from Car<T> to T” What am I missing?

  • 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-23T12:39:54+00:00Added an answer on May 23, 2026 at 12:39 pm

    When attempting to call

    public static void ServiceCar<C>(C carToService) where C : Car<C> 
    

    with this

    Dealer.ServiceCar<T>(this);
    

    you are passing an expression of type Car<T> to a method that wants an expression of type T. The reason it wants an expression of type T is because Dealer.ServiceCar<T> is explicitly defining the C as T, so the carToService parameter must be a T.

    However, Car<T> is not convertable to T. Why should it be? It does not inherit from T. The only thing it inherits from is object. Therefore, the compiler cannot convert an expression of type Car<T> to an expression of type T, just as indicated.

    To be clear, the documentation that you site and your class definition states that T must inherit from Car<T>, but it does not say the inverse, that Car<T> must inherit from T.

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

Sidebar

Related Questions

I am having some issues with a thrown exception. Below is the draw method
Having some issues with the ... in ObjectiveC. I'm basically wrapping a method and
I am having some issues with using the OrderBy extension method on a LINQ
I've been having some issues with calling -becomeFirstResponder on a UITextField contained with a
I'm having some issues with implementing a logarithm class with operator overloading in C++.
I have been having some issues retrieving JSON data from a WCF service application
I'm having some issues calling a function on an object whose name is determined
Having some issues querying back from Mongo on Id when it's a guid. I
Ok I am still having some issues with this code below. I have received
Having some issues accessing my param tags from within actionscript. Essentially I have the

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.