views:

363

answers:

7

I have a line of code that looks like this:

if (obj is byte || obj is int || obj is long || obj is decimal || obj is double || obj is float)

Is it possible to write something more elegant than this? Something like:

if (obj is byte, int, long)

I know that my example isn't possible, but is there a way to make this look "cleaner"?

+3  A: 

Why don't you do this?

bool IsRequestedType(object obj)
{
    if (obj is byte || obj is int || obj is long || obj is decimal || obj is double || obj is float)
         return true;
    return false;
}

Or you might be able to get away with

obj is IComparable
Daniel A. White
cmon, `if (true) return true; else return false;` - Ishy.
jjnguy
I just did it quickly.
Daniel A. White
Or simply: bool IsRequestedType(object obj){ return obj is byte || obj is int || obj is long || obj is decimal || obj is double || obj is float;}
Arkain
obj is IComparable would also allow string... probably not what he wants.
R. Bemrose
@arkain, that is what i did in my answer.
jjnguy
@ jinguy, hadn't scrolled that far down yet, when I wrote, I have upvoted you.
Arkain
+8  A: 

I would throw it into a method to simplify it a bit:

private static bool ObjIsNumber(object obj)
{
    return  (obj is byte || obj is int || obj is long || 
             obj is decimal || obj is double || obj is float);
}
jjnguy
You beat me to it. Upvoting you.
Chris Lively
TY !
jjnguy
+1  A: 

That looks fine to me - nice and clear.

Kragen
+12  A: 

Only:

static readonly HashSet<Type> types = new HashSet<Type> 
    { typeof(byte), typeof(int), typeof(long) etc };

...

if (types.Contains(obj.GetType())
{
}

Or use obj.GetType().GetTypeCode().

Jon Skeet
+23  A: 

You could write an extension method on object to give you syntax like:

if (obj.Is<byte, int, long>()) { ... }

Something like this (use multiple versions for fewer or more generic arguments:

public static bool Is<T1, T2, T3>(this object o)
{
    return o is T1 || o is T2 || o is T3;
}
Jason
Thats a cool use of extensions and generics!
Daniel A. White
I would name the method IsOneOf, but this is a very concise syntax indeed.
jeroenh
Quite clever, +1 from me
Arkain
Top answer! I like it
ThePower
This is very cool - thanks!
Jon Tackabury
shame you can't use something like params for generic parameters
Matthew Whited
@Matthew, I wonder if one could define `interface ITuple` and then subclass it with a variety of tuple generic classes (`Tuple<T> : ITuple`, `Tuple<T1,T2> : ITuple`, etc). Then redo the extension method as `Is<TTuple> where TTuple : ITuple`.. Then unwrap the generic type arguments inside of the method.. This would give `params`-like generic argument functionality...
Jason
+1  A: 

Create a helper function to put your test in.

Something like

public static Boolean IsNumeric(Object myObject) {
    return (obj is byte || obj is int || obj is long || obj is decimal || obj is double|| obj is float);
}
Chris Lively
+1  A: 
public static bool IsOneOf(object o, params Type[] types)
{
 foreach(Type t in types)
 {
  if(o.GetType() == t) return true; 
 }

 return false;
}

long l = 10;
double d = 10;
string s = "blah";

Console.WriteLine(IsOneOf(l, typeof(long), typeof(double))); // true
Console.WriteLine(IsOneOf(d, typeof(long), typeof(double))); // true
Console.WriteLine(IsOneOf(s, typeof(long), typeof(double))); // false
Chris Doggett