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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:50:01+00:00 2026-06-11T16:50:01+00:00

Can I somehow have overloaded methods which differ only by generic type constraints ?

  • 0

Can I somehow have overloaded methods which differ only by generic type constraints?

This does not compile:

    void Foo<T>(T bar) where T : class
    {

    }

    void Foo<T>(T bar) where T : struct
    {

    }

Since these are “open” methods, the actual method should be closed/constructed/fully-defined when it’s referenced elsewhere in code with a concretely-typed T, and then it would be clear which overload to call.

The obvious solution is not to overload them, but I’m wondering why this doesn’t work in C#?

Additional question: If this is just a C# compiler constraint, does the IL allow such an overload?

  • 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-11T16:50:02+00:00Added an answer on June 11, 2026 at 4:50 pm

    Can I somehow have overloaded methods which differ only by generic type constraints?

    No. It’s not part of the method signature in terms of overloading, just like the return type isn’t.

    There are horrible ways of “pseudo-overloading” in some cases, but I wouldn’t recommend going down that path.

    For more information, you might want to read:

    • My blog post on the topic
    • Eric Lippert’s blog post on the topic
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multimodule project. Can I make it somehow work that when calling compile
Can you somehow lock an iPad to Safari and only one webpage? I've found
I've been working all day and I somehow can't get this probably easy task
SortedList<TKey, TValue>.IndexOfKey(key) returns -1 if key is not in the list. Does this mean
I have a class Exporter which has a generic method which accepts an IEnumerable<T>
So I have run into a bit of a strange problem and can not
I can't see it as mentioned here: http://www.c-sharpcorner.com/uploadfile/mahesh/dll12222005064058am/dll.aspx Do I have to activate somehow
Can I somehow disable ListView items navigation through keyboard (arrow keys), and leave navigation
Can I somehow automatically add a node to a specific nodequeue when it is
Can anyone tell me, if in PostSharp, I somehow can turn on/off tracing in

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.