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

The Archive Base Latest Questions

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

class foo { public void bar(int i) { … }; public void bar(long i)

  • 0
class foo
{
  public void bar(int i) { ... };
  public void bar(long i) { ... };
}


foo.bar(10);

I would expect this code to give me some error, or at least an warning, but not so…

What version of bar() is called, and why?

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

    The int version of bar is being called, because 10 is an int literal and the compiler will look for the method which closest matches the input variable(s). To call the long version, you’ll need to specify a long literal like so: foo.bar(10L);

    Here is a post by Eric Lippert on much more complicated versions of method overloading. I’d try and explain it, but he does a much better job and I ever could: http://blogs.msdn.com/b/ericlippert/archive/2006/04/05/odious-ambiguous-overloads-part-one.aspx

    from the C# 4.0 Specification:

    Method overloading permits multiple
    methods in the same class to have the
    same name as long as they have unique
    signatures. When compiling an
    invocation of an overloaded method,
    the compiler uses overload resolution
    to determine the specific method to
    invoke. Overload resolution finds the
    one method that best matches the
    arguments or reports an error if no
    single best match can be found. The
    following example shows overload
    resolution in effect. The comment for
    each invocation in the Main method
    shows which method is actually
    invoked.

     class Test {   
          static void F() {
            Console.WriteLine("F()");   
          }     
          static void F(object x) {
            Console.WriteLine("F(object)");     
          }
          static void F(int x) {
            Console.WriteLine("F(int)");    
          }
          static void F(double x) {
            Console.WriteLine("F(double)");     
          }
          static void F<T>(T x) {
            Console.WriteLine("F<T>(T)");   
          }
          static void F(double x, double y) {
            Console.WriteLine("F(double,double)");  
          }     
    
          static void Main() {
            F();                // Invokes F()
            F(1);           // Invokes F(int)
            F(1.0);         // Invokes F(double)
            F("abc");       // Invokes F(object)
            F((double)1);       // Invokes F(double)
            F((object)1);       // Invokes F(object)
            F<int>(1);      // Invokes F<T>(T)
            F(1, 1);        // Invokes F(double, double)
          } 
    }
    

    As shown by the example, a particular
    method can always be selected by
    explicitly casting the arguments to
    the exact parameter types and/or
    explicitly supplying type arguments.

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

Sidebar

Related Questions

class Foo { public: void bar(); }; void Foo::bar() { static int n =
Given a declaration like this: class A { public: void Foo() const; }; What
Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);
Is following code legal C++ or not? class Foo { class Bar; void HaveADrink(Bar
I saw the following snippet code: class Foo { public: void virtual func() throw
As an example: public class Foo { private Foo() {} } public class Bar
Given the following class public class Foo { public int FooId { get; set;
Let's say I have a class: class Foo { public string Bar { get
Suppose you have a collection of Foo classes: class Foo { public string Bar;
I have this class. public class Foo { public Guid Id { get; set;

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.