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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:56:38+00:00 2026-06-04T10:56:38+00:00

In the code below, I’ve defined two overloaded methods named Bar . In Foo()

  • 0

In the code below, I’ve defined two overloaded methods named Bar. In Foo(), I make three calls to Bar, and get an error on the third call.

The first two resolve to the expected overloads. The third however generated the following error:

The type 'string' cannot be used as type parameter 'T' in the generic type or method 'Bar<T>(T, string, params object[])'. There is no implicit reference conversion from 'string' to 'System.Exception'.

Clearly the third call is binding to “Bar()”, but failing to convert the first parameter to an Exception. It’s also clear that it is the second argument being a string that’s throwing off the compiler. When it’s not a string (case 2) the resolution is fine. But it seems obvious (to me) that the failing line should bind to “Bar()” (since the first parameter is unambiguously a string).

Can anyone explain why the compiler is using this binding? I would consider creative workarounds, but what I’m really looking for is an explanation of why this is happening. I spent some time with the C# language specification, but failed to find (i.e. gave up on) a clear answer. Obviously I can rename one of the Bar methods, or provide named arguments, or mark one of the parameters ref… but none of these is ideal for my specific scenario.

Not that it’s relevant, but I have written Java code that does exactly this, and the compiler had no problem.

The code:

public void Bar(string s, params object[] ps) { } // Call this "Bar()"
public void Bar<E>(E e, string s, params object[] ps) where E : Exception { } // Call this "Bar<T>()"

public void Foo()
{
    Exception e;
    Object o1, o2;

    Bar(e, "fmt {0}", o); // Resolves fine, as expected
    Bar("fmt {0} {2}", o1, o2); // Also resolves as expected
    Bar("fmt {0} {2}", "bar", o1); // Error!
}
  • 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-06-04T10:56:40+00:00Added an answer on June 4, 2026 at 10:56 am

    The answer is that overload matching does not consider constraints. As to is this in the C# spec, I’m not completely sure. The matching always uses the most specific option and string as generic T is always more specific than string as Object (since it’s matching the actual type and not a child type). See Constraints are not part of the signature on Eric Lippert’s blog.

    To make this work, if the constraint of exception is needed, use void Bar(Exception E, ...) if possible.

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

Sidebar

Related Questions

The code below gives an error: Property 'Int32 Key' is not defined for type
The code below get image from some location - and make this image compress
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
The code below gives me this mysterious error, and i cannot fathom it. I
The code below seems to only return the first two digits of my IP
Code below I have located under my MasterPage. Once one of the tabs get
The code below contains a whole bunch of error messages, and I can't for
Code below works well on Firefox - displays progress bar which progresses on every
The code below asks to input the numbers two times. public class Palindrome {
The code below is supposed to get the new url as root while 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.