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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:45:23+00:00 2026-05-11T08:45:23+00:00

Duplicate : Function overloading by return type? Maybe this is a very silly question

  • 0

Duplicate: Function overloading by return type?


Maybe this is a very silly question but I don’t understand why I can’t declare two methods that have the same signature when they have different return types.

public class MyClass {     private double d = 0;      public double MyMethod()     {         return d;     }      public string MyMethod()     {         return d.ToString();     } } 

I get a compile error that states that the class already defines a member with the same parameter types.

(Obviously the way I’m using this in my code isn’t as simple as my example code…but I think it gets the idea across.)

Am I missing something concerning OO design that makes what I’m trying to do an OOP anti-pattern? Surely the compiler should be able to determine which method I’m trying to use as long as I specifically tell it which one I want.

Given MyClass myClass = new MyClass(); I would expect the following code to work:

double d = myClass.MyMethod(); string s = myClass.MyMethod(); 

I would expect the following code to have problems:

var v = myClass.MyMethod(); 

But even in the case of var it should result in a compile error.

Can anyone see what I’m doing wrong here? I’m more than happy to be corrected. 🙂

  • 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. 2026-05-11T08:45:23+00:00Added an answer on May 11, 2026 at 8:45 am

    It’s because of type coercion.

    Say you have the following functions:

    int x(double); float x(double);  double y = x(1.0); 

    Now, which of the two prototypes should you call, especially if they do two totally different things?

    Basically, a decision was made early on in the language design to only use the function name and arguments to decide which actual function gets called, and we’re stuck with that until a new standard arrives.

    Now, you’ve tagged your question C# but I see nothing wrong with designing a language that can do what you suggest. One possibility would be to flag as an error any ambiguous commands like above and force the user to specify which should be called, such as with casting:

    int x(double); float x(double); double y = (float)(x(1.0));    // overload casting double y = float:x(1.0);       // or use new syntax (looks nicer, IMNSHO) 

    This could allow the compiler to choose the right version. This would even work for some issues that other answers have raised. You could turn the ambiguous:

    System.out.Println(myClass.MyMethod()); 

    into the specific:

    System.out.Println(string:myClass.MyMethod()); 

    This may be possible to get added to C# if it’s not too far into a standards process (and Microsoft will listen) but I don’t think much of your chances getting it added to C or C++ without an awfully large amount of effort. Maybe doing it as an extension to gcc would be easier.

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

Sidebar

Ask A Question

Stats

  • Questions 142k
  • Answers 142k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try to use a filename relative to the current files… May 12, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer Define a focus factor, the ratio of time each developer… May 12, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer The answer is that yes, it is valid, but the… May 12, 2026 at 8:20 am

Related Questions

I'm doing some exploration of operator-overloading at the moment whilst re-reading some of my
Still 'diving in' to Python, and want to make sure I'm not overlooking something.
So I am baffled by this one. I have a function that is responsible
What would be considered the best practice in duplicating [cloning] a LINQ to SQL

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.