Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this cast working?

Tags:

c#

collections

This code works:

foreach(DataColumn column in table.Columns)
{
    // do whatever
}

But this code doesn't:

(IEnumerable<DataColumn>)(table.Columns)

The .Columns property returns a DataColumnCollection which is an InternalDataCollectionBase, which implements IEnumerable, so it should work.

The error I get is

Cannot convert type 'System.Data.DataColumnCollection' to 'System.Collections.Generic.IEnumerable'

like image 747
Rag Avatar asked Aug 08 '11 18:08

Rag


1 Answers

DataColumnCollection implements IEnumerable, and each returned row is a DataColumn, but it doesn't implement IEnumerable<DataColumn>. As it doesn't implement the interface, you can't cast to the interface. As the class is sealed, the compiler knows that the value can't possibly implement the interface, so you can't even cast to it at compile-time.

Use the LINQ Cast method insted:

table.Columns.Cast<DataColumn>()

That's effectively an adapter method - each element in the column collection will be lazily cast to DataColumn as you fetch it from the result.

The reason the foreach compiles is that the compiler adds an explicit cast for you. For instance, this will compile:

foreach (string x in table.Columns)
{
}

... but it'll throw an exception at execution time.

like image 168
Jon Skeet Avatar answered Sep 28 '22 04:09

Jon Skeet