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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:40:23+00:00 2026-05-22T15:40:23+00:00

I have the following methods: void s<t>(int a, t b) { … .. .

  • 0

I have the following methods:

void s<t>(int a, t b)
{
    ...
    ..
    .
}

void s<int>(int a, int b)
{
    ...
    ..
    .
}

void s<long>(int a, long b)
{
    ...
    ..
    .
}

when I want to use it as s<long>(10,10) I see these overrides in tooltip.
s<long>(int a,int b); and s<long>(int a,long b);. But, I think i must see just s<long>(int a,long b);.

What’s wrong ? I have visual studio 2008 sp1.

Thanks

Update : I have test it in Visual Studio 2010.The result is the same.
Update : It seems it is about c# not visual studio.

  • 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-22T15:40:24+00:00Added an answer on May 22, 2026 at 3:40 pm

    You are trying to provide the type parameters you want in the generic definition directly, which is not going to work. If you wish to have versions of the method that support (for the second parameter) objects of type int, long, and T, declare 2 non-generic method overloads and one generic.

    void S(int a, int b)
    void S(int a, long b)
    void S<T>(int a, T b)
    

    Overload resolution will then call the appropriate method based on the arguments, matching the non-generic versions for exact matches on int or long (you can get the generic version even with a int or long parameter if you explicitly invoke it using a type parameter), otherwise getting the generic version.

    Examples:

    void S<T>(int a, T b)
    {
        Console.WriteLine("generic method");
    }
    
    void S(int a, int b)
    {
        Console.WriteLine("int overload");
    }
    
    void S(int a, long b)
    {
        Console.WriteLine("long overload");
    }
    

    …

    S(10, 10);
    S(10, (long)10);
    S(10, 10L);
    S(10, 10M);
    S<int>(10, 10); // uses generic
    S<long>(10, 10); // uses generic & implicit conversion
    

    Edit: To expand upon a point mentioned briefly above and further in the comments, the match for the int and long overloads needs to be exact. All other arguments will result in the selection of the generic version. If you do not have an int but want the int overload, you will need to explicitly convert the argument prior to or during the method invocation. ex: S(10, (int)myShort);.

    Similarly, if you have two versions of method C

    void C(Mammal m) { }
    void C<T>(T t) { }
    

    with

    class Tiger : Mammal { }
    

    The invocation C(new Tiger()) will result in the generic method being used. If you want the Mammal overload for the Tiger instance, you need a reference via the base class. Such as

    Mammal m = new Tiger();
    C(m); // uses mammal overload
    // or 
    Tiger t = new Tiger();
    C((Mammal)t); // uses mammal overload
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario I have the following methods: public void AddItemSecurity(int itemId, int[] userIds) public int[]
I have the following C++ method : __declspec(dllexport) void __stdcall getDoubles(int *count, double **values);
I have the following code: private void SetControlNumbers() { string controlString = ; int
Say want to store the following: typedef std::function<void(int)> MyFunctionDecl; ..in a collection: typedef std::vector<MyFunctionDecl>
Say I have the following methods: def methodA(arg, **kwargs): pass def methodB(arg, *args, **kwargs):
I have seen the following methods of putting JavaScript code in an <a> tag:
I have parsed XML using both of the following two methods... Parsing the XmlDocument
I'm converting an application to use Java 1.5 and have found the following method:
I have something similar to the following method: public ActionResult Details(int id) { var
I have the following method in my code: private bool GenerateZipFile(List<FileInfo> filesToArchive, DateTime archiveDate)

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.