This is simply an extension method which checks if a string str
contains all the values in values
.
However, there are cases when str
might be null, values
might be null, or any item in values
might be null. I am handling those cases by using the ternary ?:
, null-conditional ?.
and null-coalesce ??
operators.
Should this piece of code be refactored?
public static bool ContainsAll(this string str, string[] values)
{
return str == null ? false : values?.All(v => str.Contains(v ?? "")) ?? false;
}
string.Contains()
, hencev ?? ""
returns v, if non-null, otherwise "". \$\endgroup\$