I have a function
public List<Item> Filter(params String[] valuesOrdered)
{
//...
}
which acts as a filter on all Item
s.
public class Item
{
List<String> propertiesOrdered;
//...
}
So that if I call e.g. Filter("prop1")
, then all Item
s with "prop1"
as the first entry their properties will be matched.
If I call Filter()
, then all Item
s should be returned.
I also need to be able to match on null
values. So for instance right now I can call Filter(null, null)
and all Item
s with null, null
as their first properties will be matched.
All of this works except when I call Filter(null)
. It will be interpreted the same way as Filter()
, but the intention is that the former should return all Item
s with null
as their first property, and the latter should return all Item
s.
I tried defining an overload to deal with this
public List<Item> Filter(String value)
{
if(value == null)
return Filter(new String[] {null});
else
return Filter(value);
}
But the compiler just shows an Ambiguous Invocation
error when I call Filter(null)
.
If you drop the overload, then this:
will be called as this:
and not like this:
So, you can detect this, and if you want it to behave as though you passed an array containing a single
null
, then this will suffice:Note that this:
will in fact be called as this:
so there is a difference between the two that you can detect.
Here's a simple LINQPad program that demonstrates:
output: