I’ve got the following method:
public static List<string> GetArgsListStartsWith(string filter, bool invertSelection, bool lowercaseArgs)
{
return GetArgumentsList(lowercaseArgs)
.Where(x => !invertSelection && x.StartsWith(filter)).ToList();
}
And then I call it like this GetArgsListStartsWith("/", true, false)
That would translate to: get a list of all arguments that do not start with “/”.
The problem is that the list doesn’t get populated, even if all arguments do not start with “/”.
If I call GetArgsListStartsWith("/", false, false) which translates to: get a list of all arguments which start with “/”, the list does get populated with the arguments that start with “/”.
I suspect that !invertSelection && x.StartsWith(filter) doesn’t return true when invertSelection is set to true and x.StartsWith(filter) returns false, but I do not understand why. Does anyone see something I don’t?
As other answers have said, your condition will only ever return true when
invertSelectionis false.The simplest way of conditionally inverting a result is to use the XOR operator:
I prefer this over lc’s solution as it only specifies the
StartsWithonce.