I am looking for a convenient way to drop list items with empty string as their value.
I know I could check each string to see if it is empty before loading into the list.
List<string> items = new List<string>();
if (!string.IsNullOrEmpty(someString))
{
items.Add(someString);
}
However, this seems a bit cumbersome especially if I have a lot of strings to add to the list.
Alternatively, I could just load all the strings regardless of being empty or not:
List<string> items = new List<string>();
items.Add("one");
items.Add("");
items.Add("two")
Then iterate over the list and if an empty string is found remove it.
foreach (string item in items)
{
if (string.IsNullOrEmpty(item))
{
items.Remove(item);
}
}
Are these my only two options, perhaps there is something in Linq?
Thanks for any help with this.