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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@happysmile : Fixed the answer a bit, I was a bit wrong about the use of optional parameters with generic value types.
0

Yes, this is called generic programming see http://en.wikipedia.org/wiki/Generic_programming#Generic_programming_in_.NET

Comments

0

You can do it with generic types and for possibility of different paramaters you can use params reference and it doesn't matter parameters count that you want to pass.

    private T Addtoatal<T>(params T[] items)
    {
        dynamic total = 0;
        foreach (dynamic item in items)
        {
            total += item;
        }
        return total;
    }

Usage;

var total = Addtoatal<float>(1, 2, 3, 4, 5, 6);
var total = Addtoatal<int>(1, 2, 3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.