public int Next(int celt, FORMATETC[] rgelt, int[] pceltFetched)
{
// Start with zero fetched, in case we return early
if (pceltFetched != null && pceltFetched.Length > 0)
pceltFetched[0] = 0;
// This will count down as we fetch elements
int cReturn = celt;
// Short circuit if they didn't request any elements, or didn't
// provide room in the return array, or there are not more elements
// to enumerate.
if (celt <= 0 || rgelt == null || currentIndex >= formats.Length)
return 1; // S_FALSE
// If the number of requested elements is not one, then we must
// be able to tell the caller how many elements were fetched.
if ((pceltFetched == null || pceltFetched.Length < 1) && celt != 1)
return 1; // S_FALSE
// If the number of elements in the return array is too small, we
// throw. This is not a likely scenario, hence the exception.
if (rgelt.Length < celt)
throw new ArgumentException("The number of elements in the return array is less than the number of elements requested");
// Fetch the elements.
for (int i = 0; currentIndex < formats.Length && cReturn > 0; i++, cReturn--, currentIndex++)
rgelt[i] = formats[currentIndex];
// Return the number of elements fetched
if (pceltFetched != null && pceltFetched.Length > 0)
pceltFetched[0] = celt - cReturn;
// cReturn has the number of elements requested but not fetched.
// It will be greater than zero, if multiple elements were requested
// but we hit the end of the enumeration.
return (cReturn == 0) ? 0 : 1; // S_OK : S_FALSE
}