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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:51:43+00:00 2026-05-27T09:51:43+00:00

I ran in an overloading resolution problem which I did not expect. Apparently the

  • 0

I ran in an overloading resolution problem which I did not expect. Apparently the compiler rather resolves a call to the MyPrint method passing "Hello world" to the MyPrint<T>(T : struct) in the derived class than to the MyPrint(string) defined in the base class. And since the method that the resolver chose will not accept the string, I get a compiler error.

I suppose it has to do with the Brittle Base Class problem as Jon Skeet wrote in his book, and Eric Lippert on his blog. Lippert quotes the standard as saying:

Methods in a base class are not candidates if any method in a derived
class is applicable.

But I don’t see any method in the derived class being applicable, and neither does the compiler judging from the error. I would like to know two things: why does it work this way, and how can I let C# do what I want it to do again?

Contrived example demonstrating the problem:

class Program
{
    static void Main(string[] args)
    {
        Derived d = new Derived();

        d.MyPrint(10);

        // ERROR: The type 'string' must be a non-nullable value type
        // in order to use it as parameter 'T'
        // in the generic type or method 'Derived.MyPrint<T>(T)'.
        d.MyPrint("Hello world!");
    }
}

class Base
{
    // I print only strings.
    public void MyPrint(string value)
    {
        Console.WriteLine("String: " + value);
    }
}

class Derived : Base
{
    // I print any primitive type, enums and DateTime,
    // but NOT string (since that is not a value type).
    public void MyPrint<T>(T value)
        where T : struct, IConvertible
    {
        Console.WriteLine("IConvertible: " + value.ToString());
    }
}
  • 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-27T09:51:43+00:00Added an answer on May 27, 2026 at 9:51 am

    You already found half of the reason, but you also need Constraints are not part of the signature. Combined with “Methods in a base class are not candidates if any method in a derived class is applicable”, you get that during overload resolution there are two methods that look like MyPrint(string) and are therefore applicable, so the one in the most-derived class is chosen. Only after that is the constraint checked, which then fails.

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

Sidebar

Related Questions

Ran into a interesting little problem. I was writing a method to filter an
Ran into a problem. I have a spawned thread that must call its finally
Ran into another problem using SSL and Tomcat: I've configured a keystore which contains
My first time overloading the [] operator for something practical and I ran into
I ran into a problem using Springsource Tool Suite when writing some groovy scripts
I ran into a scenario where I had a delegate callback which could occur
I ran into a problem that suggests I may be implementing a design pattern
I ran into a strange issue with tinyMCE that i was not able to
Ran into a situation where am not sure how to handle it. I have
Ran into an interesting problem with a MySQL table I was building as 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.