Is there a default method defined in .Net for C# to remove all the elements within a list which are null
?
List parame
There is another simple and elegant option:
parameters.OfType<EmailParameterClass>();
This will remove all elements that are not of type EmailParameterClass
which will obviously filter out any elements of type null
.
Here's a test:
class Test { }
class Program
{
static void Main(string[] args)
{
var list = new List<Test>();
list.Add(null);
Console.WriteLine(list.OfType<Test>().Count());// 0
list.Add(new Test());
Console.WriteLine(list.OfType<Test>().Count());// 1
Test test = null;
list.Add(test);
Console.WriteLine(list.OfType<Test>().Count());// 1
Console.ReadKey();
}
}