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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:10:25+00:00 2026-05-24T06:10:25+00:00

In an interview they had asked an Question like this there are three overloading

  • 0

In an interview they had asked an Question like this
there are three overloading methods

public int Addtoatal(int a, int b)
public int Addtoatal(int a, int b, int c)
public float Addtoatal(float a, float b)

is there any way i can write down one method where i can send an int or float as an parameter to the method ,can we really do any thing like this in OOPS

any help would be great

Thanks

  • 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-24T06:10:26+00:00Added an answer on May 24, 2026 at 6:10 am

    Yes, using generics:

    public T Addtoatal<T>(T a, T b) {}
    

    If the method needs to use any operators (+,-,*) etc. it will be problematic because generic types can’t ensure that the class T has the operator (operators can’t be declared in interfaces, and therefor you can’t make a constraint to match).
    In .Net 4.0 you could use dynamic typing to get a dynamic variable to contain the result and add to it:

    public T Addtoatal<T>(T a, T b) {
       dynamic res = a;
       a += b;
       return res;
    }
    

    In other .Net versions you’ll need a work around like the Operator class in MiscUtils: generic method to add int, float, in c#


    The second example with the three parameters would need to have a different signature (in .Net 2.0) because it has more parameters.

    in .Net 4.0 you could just use optional parameters for generic types, but can’t do it for value types:

    public T Addtoatal<T>(T a, T b, T c = null) where T: class { //Do stuff }
    

    But you could use boxing with nullable types, this would work with int and double:

    public T Addtoatal<T>(T? a, T? b, T? c = null) where T : struct { //Do stuff, use .Value }
    

    Now the next best thing you could do, if you don’t want to use generics, is have a single method declaration as double (C# 4.0, because of the optional parameter):

    public double Addtoatal(double  a, double  b, double c = 0) {}
    

    In this example the int values will be converted to a double anyway, so there’s no loss of data, and the int can implicitly converted to double, but the return value would need to be explicitly cast to int.

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

Sidebar

Related Questions

I was asked this question during an interview. They're both O(nlogn) and yet most
This was an job placement interview I faced. They asked whether we can realloc
I had telephone interview question yesterday. The interviewer asked me if I had faced
This was an interview question. Given Visual Studio 2008 and an icon saved as
My Friend faced an interview of IT company where they asked him Give Practical
I was at an interview for a C position in which they presented me
In a past interview, I was asked how would I write a mission critical
recently I had a job interview. I had two tasks: 1) to refactor a
I had a few questions in a technical interview that I thought I knew,
Gosh I had right now a hell of an interview. No matter how much

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.