DataTableReader.GetEnumerator Method

Definition

Returns an enumerator that can be used to iterate through the item collection.

C#
public override System.Collections.IEnumerator GetEnumerator();

Returns

An IEnumerator object that represents the item collection.

Exceptions

An attempt was made to read or access a column in a closed DataTableReader.

Examples

The following example demonstrates the use of the GetEnumerator method. This includes the behavior of the enumerator when rows are deleted from the underlying DataTable while the enumerator is active.

C#
public static void Main()
{
    try
    {
        DataTable userTable = new DataTable("peopleTable");

        userTable.Columns.Add("Id", typeof(int));
        userTable.Columns.Add("Name", typeof(string));

        // Note that even if you create the DataTableReader
        // before adding the rows, the enumerator can still
        // visit all the rows.
        DataTableReader reader = userTable.CreateDataReader();
        userTable.Rows.Add(new object[] { 1, "Peter" });
        userTable.Rows.Add(new object[] { 2, "Mary" });
        userTable.Rows.Add(new object[] { 3, "Andy" });
        userTable.Rows.Add(new object[] { 4, "Russ" });

        IEnumerator enumerator = reader.GetEnumerator();
        // Keep track of whether the row to be deleted
        // has actually been deleted yet. This allows
        // this sample to demonstrate that the enumerator
        // is able to survive row deletion.
        bool isRowDeleted = false;
        while (enumerator.MoveNext())
        {
            DbDataRecord dataRecord = (DbDataRecord)enumerator.Current;

            // While the enumerator is active, delete a row.
            // This doesn't affect the behavior of the enumerator.
            if (!isRowDeleted)
            {
                isRowDeleted = true;
                userTable.Rows[2].Delete();
            }
            Console.WriteLine(dataRecord.GetString(1));
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    Console.ReadLine();
}

The procedure displays the following text in the Console window:

Peter  
Mary  
Russ  

Remarks

Enumerators only allow for reading the data in the DataTableReader. Enumerators cannot be used to modify the underlying collection.

At first, the enumerator is positioned before the first element in the collection. At this position, calling Current throws an exception. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current.

Current returns a DbDataRecord, and returns the same object until either MoveNext or Reset is called. MoveNext sets Current to the next element.

After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling MoveNext returns false. If the last call to MoveNext returned false, calling Current throws an exception. In addition, because the DataTableReader provides forward-only access to its data, calling the Reset method of the IEnumerator throws a NotSupportedException.

The DataTableReader provides a stable enumerator. This means that even if row deletions or additions occur within the underlying data, the enumerator returned by a call to GetEnumerator is still valid.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1