If you want to use a foreach (and it makes some sense) and assuming there is enough space in the
result
array, then you just have to set up an integer of your own:
int j = 0;
foreach (String s in properties)
{
if (searchresult.Properties.Contains(s))
{
if (searchresult.Properties[s][0].ToString() != null)
{
result[j++]= searchresult.Properties[s][0].ToString();
}
}
}
Alternatively, I would set up
result
as a List of strings, and add to it each time:
foreach (String s in properties)
{
if (searchresult.Properties.Contains(s))
{
if (searchresult.Properties[s][0].ToString() != null)
{
result.Add( searchresult.Properties[s][0].ToString());
}
}
}
If you need an array afterwards, then List has a ToArray method. This way, you are not assigning space you don't need, or leaving null entries at the end of the array, or running out of array because you didn't assign enough elements.