I have this:
List<string> s = new List<string>{"", "a", "", "b", "", "c"};
I want to remove all the empty elements ("") from it quickly (probably through LINQ) without using a foreach statement because that makes the code look ugly.
You can use List.RemoveAll:
C#
s.RemoveAll(str => String.IsNullOrEmpty(str));
VB.NET
s.RemoveAll(Function(str) String.IsNullOrEmpty(str))
s.RemoveAll(String.IsNullOrEmpty);, you don't need a lambda in this case.s.RemoveAll(AddressOf String.IsNullOrEmpty) and the lamdba shows that it's possible to modify it easily. Imho it's more readable with the lambda.int count = s.RemoveAll(string.IsNullOrEmpty); is valid but List<string> list = s.RemoveAll(string.IsNullOrEmpty); not.Check out with List.RemoveAll with String.IsNullOrEmpty() method;
Indicates whether the specified string is null or an Empty string.
s.RemoveAll(str => string.IsNullOrEmpty(str));
Here is a DEMO.
s = s.Where(val => !string.IsNullOrEmpty(val)).ToList();
stringarray but a list.