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

  • Home
  • SEARCH
  • 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 8731539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:15:43+00:00 2026-06-13T09:15:43+00:00

Possible Duplicate: Generic methods and method overloading Ok, I hit this one by accident…

  • 0

Possible Duplicate:
Generic methods and method overloading

Ok, I hit this one by accident… Giving this situation:

class Program {
    static void Main( string[ ] args ) {

        var obj = new gen<int>( );
        Console.Write( obj[ 1 ] );
        Console.ReadKey( );

    }
}

class gen<T> {

    public int this[ T i ] { get { return 2; } }

    public int this[ int i ] { get { return 1; } }

}

It will always print 1. I would have expected for the compiler to complain, or the runtime to crash and burn and melt the CPU, but no, it is happy to print ‘1’

Of course I can make a choice to return either if I use any other type for the generic parameter. For giggles, I try using UInt as the generic type parmater, and I can differentiate between the calls, so the questions are:

  1. Why C# does not freak out? Shouldn’t Anders Hejlsberg feel a disturbance in the force?

  2. How can I restrict a generic parameter from certain types? As in this T can be anything but ints (but long are OK)

  • 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-13T09:15:44+00:00Added an answer on June 13, 2026 at 9:15 am

    I believe this is specified in section 7.5.3.2 of the C# 4 spec (Better Function Member).

    • Otherwise, if MP has more specific parameter types than MQ, then MP is better than MQ […]
      • A type parameter is less specific than a nontype parameter
      • […]

    So here, the member with the T parameter is less specific than the member with the int parameter.

    Try to design your way out of this one simply by not overloading like this. It’s hard for indexers of course, but you could always provide methods instead (or possibly as well, as a fall-back).

    EDIT: Note that if you have overloaded methods where both are type parameters, the compiler will complain:

    public class Foo<T1, T2>
    {
        public void Bar(T1 t1) {}
        public void Bar(T2 t2) {}
    }
    
    ...
    
    Foo<int, int> foo = new Foo<int, int>();
    foo.Bar(10); // Error
    

    Here neither method is more specific.

    How can I restrict a generic parameter from certain types? As in this T can be anything but ints (but long are OK)

    This is really an entirely separate question, but basically you can’t. You can constrain type parameters in various ways, but not by explicitly including and excluding types. See the MSDN page on constraints for more information.

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

Sidebar

Related Questions

Possible Duplicate: No type inference with generic extension method Consider two methods: public static
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic This
Possible Duplicate: Generic methods and multiple constraints I need a generic function that has
Possible Duplicate: How to determine the class of a generic type? I have a
Possible Duplicate: Generic constraints, where T : struct and where T : class Is
Possible Duplicate: Determining the Type for a generic method parameter at runtime Generics in
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic This
Possible Duplicate: C#: How to reference generic classes and methods in xml documentation I
Possible Duplicate: Catching specific vs. generic exceptions in c# Here's an example method private
Possible Duplicate: Why won't this generic java code compile? Given the following code: import

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.