1

I've been wondering about this: would you support using simply an object as a parameter in your method? My reason behind doing this would be overloading. Currently, I'm trying to create a method that caters to many different datatypes: string, decimal, DateTime... the list goes on.

It's getting a bit messy though, so I was thinking of doing the following

public void GenericMethod(object val)
{
    if (val is string)
        // process as string
    else if (val is decimal)
        // process as decimal
    else if (val is DateTime)
        // do something for dt
    else
        // ...
}

What do you think of such a method? Would it incur unnecessary overhead? (during type checking) Have you implemented it? Tell me...

EDIT: Yeah, and just a sidenote, I'm kinda familiar with overloading. But it gets a little annoying when there are like more than 10 overloads...

2
  • 1
    While that technically works it's certainly not a good idea unless you have a very compelling reason to do so. Commented May 22, 2012 at 16:43
  • It ultimately depends on what you're doing in your method and how different your method becomes for each type. Ideally you'd actually use generics and make the parameter generic if they're all similar. If they're radically different, then this is definitely not a good idea. It's impossible to tell what would be appropriate in your case. Commented May 22, 2012 at 16:50

6 Answers 6

9

Yes, that would work. However there are better ways of doing this.

Your best bet is to use overloads:

public void GenericMethod(string val)
{
        // process as string
}
public void GenericMethod(decimal val)
{
    // process as decimal
}

etc.

Whenever you use a is keyword in your code that's a huge hint that you're probably forgetting to use some important O.O. principles: overloads, subclasses, or the like.

Overloads aren't actually that annoying to work with, just to write. Remember, you're not coding this for yourself today, you're coding this for yourself three months from now when you have to read the code and figure out why the heck you did it that way, or where in the world that bug comes from.

Yet another reason to avoid the "switch on type" technique is for consistency with the .NET framework (and thus people's expectations). Follow Console.Write and the other wide variety of methods that are overridden within and under a given class.

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

2 Comments

Yeah, we've all been through the "why the heck you did it that way" and "where in the world that bug comes from" situations. I was thinking that other people might find it more effective to have simply one method to work with, instead of having to work their way through many overloads.
@matt...not really though. I'm used to how .NET does things: one overload per type, and if you tried to change it up I'd get pretty confused.
7

I've been wondering about this: would you support using simply an object as a parameter in your method?

Very rarely. If there's a fixed set of types which are properly supported - and you'll throw an exception otherwise - then I'd use overloads.

If you can actually accept any type, and you'll handle a not-specially-supported type in some well-known way, then it's okay to accept object. That's what LINQ to XML does all over the place, and the result is a very clean API. I'd do it very carefully though - it's rarely a good idea.

And yes, there'd be an overhead. I wouldn't usually make that the basis of the decision though - the overhead will be small enough to be negligible in most cases. Design your API as cleanly as you can, then work out whether it's going to cause a bottleneck.

Comments

3

Yes, it would incur overhead for both type checking and boxing/unboxing the value type. I would recommend the overloads.

Another possibility, as long as you aren't doing a lot of math with the number, would be to make it a generic method. Arithmetic is rather difficult with generics though, as there are no constraints for value types which enables the use of operators.

Comments

3

No need of those!

Just declare as many methods with same name as you want and take each type as argument in each method.[This is called Overloading. e.g. You may have seen that +1 Overloads beside Methods, which implies that there is 1 more Method with same name but with different argument types]

Say Like this :

void Method(decimal d)
{
    //Process Decimal
}
void Method(string s)
{
    //Process String
}

By default, It will find its own method according to the Type.

Comments

1

There are cases where your approach makes sense. I've used it before, mostly when I have a bunch of processing that is the same for different data types.

But this is not overloading. Overloading would be to define different signatures for the same method name like this:

public void GenericMethod(string val)
{
    // process as string
}

public void GenericMethod(decimal val)
{
    // process as decimal
}

public void GenericMethod(DateTime val)
{
    // do something for dt
}

// Etc.

And for some cases, this approach makes more sense.

1 Comment

Looks like the weak-minded are out, downvoting answers without the grapes to explain why.
0

Implementing many overloads one of them takes object is no problem. Take a look at Console.WriteLine overloads for example. http://msdn.microsoft.com/en-us/library/system.console.writeline.aspx However, take care that int for example can conflict with double:

int sum(int i, int j)
{
 return i + j;
}

double sum(double i, double j)
{
 return i + j;
}

object sum(object i, object j)
{
  return i.ToString() + j.ToString();
}

==============================

static void Main()
{
   sum(1, 2); // Error: ambigous call between `int` and `double` versions
   sum(1.0, 2.0); // calls double version, although 1.0 and 2.0 are objects too
   sum("Hello", "World"); // object
}

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.