Is there an inline IEqualityComparer for .Contains?

  • Thread starter Thread starter RayLopez99
  • Start date Start date
R

RayLopez99

Thanks to Goran, I figured out a while ago how to use a method for
IEqualityComparer. Also I've figured out how to use LINQ. But for
shorthand, I'd like to know if there's an "inline" way of
using .Contains on an array to find a value.

Say you have an array: AnArrayContainingIntegers.

You want to find if any number less than or equal to "13" exists in
this array.

Can you do this *without* using a method for IEqualityComparer? Can
you do it, in other words, inline?

I've tried various things but nothing compiles.

so:

int testInteger = 13;

bool testBool = AnArrayContainingIntegers.Contains(13,
IEqualityComparer<int> comparer);

is the template, but what to put on the RHS for IEqualityComparer<int>
comparer?

Again, I can do this using a method/function with template
IEqualityComparer, but I'm trying to see if there's a way to do it
"inline".

Also using LINQ I can do this along the lines of AnArray.Where (x => x
<= testInteger), etc, but I'm looking for using IEqualityComparer in
this example (something for my library).

RL
 
RayLopez99 said:
Thanks to Goran, I figured out a while ago how to use a method for
IEqualityComparer. Also I've figured out how to use LINQ. But for
shorthand, I'd like to know if there's an "inline" way of
using .Contains on an array to find a value.

Say you have an array: AnArrayContainingIntegers.

You want to find if any number less than or equal to "13" exists in
this array.

Can you do this *without* using a method for IEqualityComparer? Can
you do it, in other words, inline?

I've tried various things but nothing compiles.

so:

int testInteger = 13;

bool testBool = AnArrayContainingIntegers.Contains(13,
IEqualityComparer<int> comparer);

is the template, but what to put on the RHS for IEqualityComparer<int>
comparer?

Again, I can do this using a method/function with template
IEqualityComparer, but I'm trying to see if there's a way to do it
"inline".

Also using LINQ I can do this along the lines of AnArray.Where (x => x
<= testInteger), etc, but I'm looking for using IEqualityComparer in
this example (something for my library).

RL

As IEqualityComparer<int> is an interface, you have to have an instance
of a class implementing the interface. That can't be created inline.

You could use the extension method Any:

bool testBool = AnArrayContainingIntegers.Any(x => x <= testInteger);
 
You could use the extension method Any:

bool testBool = AnArrayContainingIntegers.Any(x => x <= testInteger);

Thanks Goran, I've added your comment to my library and noted the
LINQ .Any command, which I had not realized before.

RL
 
Back
Top