Introduction to the System. Data. DataRowCollection class

Source: Internet
Author: User

Let's take a look at the following program:

01:  using System;02:  using System.Data;03:  using System.Linq;04:  05:  namespace Skyiv.Ben06:  {07:    class Program08:    {09:      static void Main()10:      {11:        var dt = new DataTable();12:        dt.Rows.Add();13:        foreach (var row in dt.Select())14:        {15:          Console.WriteLine(row.GetType());16:          Console.WriteLine(row.RowState);17:        }18:      }19:    }20:  }

The type of the Rows attribute of the able class in the first row of the above program is the DataRowCollection class. The Select method of the able class in row 13th returns the DataRow [] array. We know that the foreach statement repeats a set of embedded statements for the array or each element in the object set that implements the System. Collections. IEnumerable or System. Collections. Generic. IEnumerable <T> interface. Therefore, the type of the row variable in the foreach statement is inferred as the DataRow class. The running result of this program is as follows:

System.Data.DataRowAdded

 

If you change the 13th rows in the above program:

13:        foreach (var row in dt.Rows)

The following compilation errors will occur:

Program. cs (): error CS1061: "object" does not contain the definition of "RowState, and the extended method "RowState" of the first parameter of the acceptable type "object" cannot be found (is the using command or assembly reference missing ?)

 

This is because the DataRowCollection class only implements System. collections. the IEnumerable interface is not implemented. collections. generic. IEnumerable <DataRow> interface, so the type of the row variable in the foreach statement is inferred as System. object Class.

There are many collection classes that only implement the IEmnumerable interface but not the IEnumerable <T> interface. For example:

  • System. Data. DataColumnCollection
  • System. Data. ConstraintCollection
  • System. Data. Common. DataTableMappingCollection
  • System. Windows. Forms. DataGridViewRowCollection
  • System. Web. UI. WebControls. GridViewRowCollection

Because generics are introduced in. NET 2.0, most of the above classes are already in. NET 1.0. However, Microsoft must implement the IEnumerable <T> interface for the above class after. NET 2.0. This is a pity of. NET Framework Base Class Library. Please refer to a related post on the MSDN Forum: Missing linq extension method.

Fortunately. in NET Framework 3.5 and later versions. IEnumerable is added to the Linq namespace. cast <TResult> extension method, which can convert IEnumerable elements to the specified TResult type. In this way, the first line of the above program is changed:

13:        foreach (var row in dt.Rows.Cast<DataRow>())

It will work normally. Of course, there is a better solution in this case, that is, to change the 13th rows:

13:        foreach (DataRow row in dt.Rows)

You can.

The good news is that the Collection <T> class in the System. Collections. ObjectModel namespace implements the IEnumerable <T> interface, while the Collection <T> class has many derived classes. For example, the IPEndPointCollection class in the System. Net namespace inherits from the Collection <IPEndPoint> class, and thus implements the IEnumerable <IPEndPoint> interface.

References
  1. Missing linq extension method
  2. MSDN: foreach, in (C # Reference)
  3. MSDN: DataRowCollection class (System. Data)
  4. MSDN: IPEndPointCollection class (System. Net)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.