I have a list of variables declared for a string value. It is from an external source, there could be 5-10 variables. How do I check if all of them do not contain a certain string ("K.K")? I do not want to check for each such as
public string string_1;
public string string_2;
public string string_3;
...
...
...
if(string_1!= "K.K") //for all the strings for non-array collection
{}
...
...
...
If you know how do get your variables into an array/List
var strings = new List<string>();
strings.Add(string_1);
strings.Add(string_2);
strings.Add(string_3);
...
or
var strings = new List<string>{string_1, string_2, string_3, ...};
or
var strings = new []{string_1, string_2, string_3, ...};
you could e.g. use Linq Any and as filter string.Contains or also string.Equals depending on your needs
if(!strings.Any(s => s.Contains("K.K")))
{
...
}
Alternatively to using Equals if you know your strings should all be unique you could also use
var strings = new HashSet<string>();
strings.Add(string_1);
strings.Add(string_2);
strings.Add(string_3);
...
or
var strings = new HashSet<string>{string_1, string_2, string_3, ...};
and then simply check
if(!strings.Contains("K.K"))
{
...
}