Could the problem be that you are searching for white space instead of an empty string?

Try below:

for(int i = 0; i < allToAddresses.Length; i++)
   if(allToAddresses[i] == "") // find where empty element is
   { //Here i am trying to delete that empty element. does not work


If you are using arrays, you will need to pull the valid values out and put them into a new instance of an array. You can do something like this:

internal static T[] RemoveNullArrayElements<T>(T[] array)
        if (array != null)
            List<T> newLst = new List<T>();
            foreach (var ar in array)
                if (ar != null)
            return newLst.ToArray();

        return array;


You cannot truly "remove" elements from an array, because array size is fixed*. You can, however, construct a new array that skips all empty elements:

allToAddresses = allToAddresses.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();

The above requires using System.Linq at the top of your file. It checks all entries in your array to see if they are null or consist entirely of white space (spaces, tabs, etc.) and produces a new array of strings, containing only non-empty / non-null entries from the original array.

* In the interest of full disclosure, .NET does have an API that lets you modify array size, but you should not use it in situations like this.


test =  test.Where(x => !string.IsNullOrWhitepace(x)).ToArray();


You could try to use Linq for this

allToAddresses = allToAddresses.Where(address=>!string.IsNullOrWhiteSpace(address))

You have to include also this in your namespaces:

using System.Linq;

You filter your initial array using the Where method. In this method you pass a predicate that returns true if for the current address the method string.IsNullOrWhiteSpace returns false. Otherwise it returns false. Using this filter you discard the addresses that are null, empty, or consisted only of white-space characters.

Related Query

More Query from same tag