Sometimes when we are fetching values from database into an array we might get blank/null values also. In order to remove the blank/null values,use the below code.
Sample array having blank and null values:
string[] myColors = { "Red", "Blue", "", "Green", "", null, "pink" };
using LINQ:
myColors = myColors.Where(color => !string.IsNullOrEmpty(color)).ToArray();
with out LINQ:
List<string> tempListColors = new List<string>();
foreach (string color in myColors)
{
if (!string.IsNullOrEmpty(color))
tempListColors.Add(color);
}
myColors = tempListColors.ToArray();
output:
Array of count=4 with values as "Red", "Blue","Green", "pink"