471,357 Members | 1,083 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

Passing in operators

Tem
I have 3 static methods that share almost the same code with only a few
lines where different operators are used.

Is there a way to pass in an operator as a parameter?

pseudo code

Greater() {Base(>)}
Less() {Base(<)}
Equal() {Base(==)}

Base(operator op)
{
....

if(a op b)
....
}

Or another way of doing this?

Tem

Jun 27 '08 #1
6 6959
Or another way of doing this?

Depending on what you are doing, you could use a delegate - i.e. a
Func<T,T,bool>; whether this is a good idea (or not) depends on the
scenario... easy enough in C# 3 with lambdas...

public static void Greater() {Base((x,y) =x y);}
public static void Less() {Base((x,y) =x < y);}
public static void Equal() {Base((x,y) =x == y);}

private static void Base(Func<Foo,Foo,boolop)
{
//...
if(op(a,b))
{
//...
}
}

(again: I stress hard to give a great answer without more info)

Marc
Jun 27 '08 #2
I should also add that for equaility (==, !=) an inequality (<, <=, >,
>=) you can also use EqualityComparer<T>.Default and
Comparer<T>.Default to do a lot, without requiring direct access to
operators (actually it uses IComparable<Tand IEquatable<T>).

Marc
Jun 27 '08 #3
"Tem" <te*****@yahoo.comwrote:
I have 3 static methods that share almost the same code with only a
few lines where different operators are used. Is there a way to pass
in an operator as a parameter?
What you can do is pass in a delegate (method pointer) that performs the
desired comparison, e.g.
delegate bool CompareDelegate(int x, int y);
static void Main()
{
CompareDelegate lessThan = delegate(int x, int y) { return x < y; };
CompareDelegate moreThan = delegate(int x, int y) { return x y; };

bool isLess = Compare(2, 5, lessThan); // true
bool isMore = Compare(2, 5, moreThan); // false
}
static bool Compare(int a, int b, CompareDelegate c)
{
return c(a, b);
}
Eq.
Jun 27 '08 #4
Tem
Thank you guys for the help. Now I see what delegates are useful for.

"Paul E Collins" <fi******************@CL4.orgwrote in message
news:l5******************************@bt.com...
"Tem" <te*****@yahoo.comwrote:
>I have 3 static methods that share almost the same code with only a few
lines where different operators are used. Is there a way to pass in an
operator as a parameter?

What you can do is pass in a delegate (method pointer) that performs the
desired comparison, e.g.
delegate bool CompareDelegate(int x, int y);
static void Main()
{
CompareDelegate lessThan = delegate(int x, int y) { return x < y; };
CompareDelegate moreThan = delegate(int x, int y) { return x y; };

bool isLess = Compare(2, 5, lessThan); // true
bool isMore = Compare(2, 5, moreThan); // false
}
static bool Compare(int a, int b, CompareDelegate c)
{
return c(a, b);
}
Eq.

Jun 27 '08 #5
Tem
Can this be written with lambda expressions?

"Paul E Collins" <fi******************@CL4.orgwrote in message
news:l5******************************@bt.com...
"Tem" <te*****@yahoo.comwrote:
>I have 3 static methods that share almost the same code with only a few
lines where different operators are used. Is there a way to pass in an
operator as a parameter?

What you can do is pass in a delegate (method pointer) that performs the
desired comparison, e.g.
delegate bool CompareDelegate(int x, int y);
static void Main()
{
CompareDelegate lessThan = delegate(int x, int y) { return x < y; };
CompareDelegate moreThan = delegate(int x, int y) { return x y; };

bool isLess = Compare(2, 5, lessThan); // true
bool isMore = Compare(2, 5, moreThan); // false
}
static bool Compare(int a, int b, CompareDelegate c)
{
return c(a, b);
}
Eq.

Jun 27 '08 #6
Can this be written with lambda expressions?

yes; I aleady posted a variant using lambdas (although using the
"delegate" version of a lambda*); using an actual Expression is
possible - not sure if it gains much, though (the delegate is likely
to be quicker than reflection from a PropertyInfo).

Whether this is the *best* approach depends largely on the full
scenario.

*=lambdas can be compiled to either a delegate or a System.Expression.

Marc
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.