473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Explicit specification of range variable required when hiding inherited IEnumerable<T>

Hello

I'm developing an API with a number of concrete collections that inherit
from an abstract collection hierarchy. Each inheritance level provides an
implementation of IEnumerable<T>, thus allowing the developer to take full
advantage of the LINQ extensions without having to start with a cast
operation to a concrete type that matches the collection.

I was surprised that the C# compiler was unable to resolve the type of the
range variable when writing LINQ queries against a collection that hides an
inherited IEnumerable<T>. Consider the following example (just to be clear -
I'm fully aware that it is possible to write the following using a generic
collection instead, but please follow me);

public class Animal {}
public class Tiger : Animal {}

public abstract class AnimalCollection : IEnumerable<Animal>
{
public IEnumerator<AnimalGetEnumerator()
{
throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}

public class TigerCollection : AnimalCollection, IEnumerable<Tiger>
{
public new IEnumerator<TigerGetEnumerator()
{
throw new NotImplementedException();
}
}

public void ProcessAnimals()
{
AnimalCollection animals = new TigerCollection();
TigerCollection tigers = new TigerCollection();

// works
var a1 = from a in animals select a;

// fails (requires explicit specification of t as Tiger)
var t1 = from t in tigers select t;

// works when specifying Tiger explicitly
var t1 = from Tiger t in tigers select t;
}

Error 1 Could not find an implementation of the query pattern for source
type 'TigerCollection'. 'Select' not found. Consider explicitly specifying
the type of the range variable 't'.

We were just wondering why the C# compiler is unable to resolve the type
Tiger from the TigerCollection (now that it explicit implements
IEnumerable<Tiger>.

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)

Sep 5 '08 #1
5 1776
From the compiler's perspective, it has both IEnumerable<Tigerand
IEnumerable<Animalto pick from, and neither is "better", so it can't
make the decision on its own. Similarly, if you declare:

static void Test(IEnumerable<Animalanimals) {}
static void Test(IEnumerable<Tigertigers) {}

and call Test(tigers);

The call is ambiguous between the following methods or properties:
'Program.Test(System.Collections.Generic.IEnumerab le<Animal>)' and
'Program.Test(System.Collections.Generic.IEnumerab le<Tiger>)'

but add a static void Test(TigerCollection tigers) { } and it can do
it.

But it tells you how to fix it (and it is easy to do so), which is
usually enough...

Marc
Sep 5 '08 #2
To clarify - it *would* be able to pick a better candidate if the
difference was between Foo<Tand Bar<Twhere Foo<T: Bar<T>, but
this is very different to SomeType<Foovs SomeType<Barwhere Foo :
Bar.

Gotta love variance ;-p

Marc
Sep 5 '08 #3
From the compiler's perspective, it has both IEnumerable<Tigerand
IEnumerable<Animalto pick from, and neither is "better".
Well, I'm explicit asking for the enumerable object of IEnumerable<Tiger>. I
guess this one should be better than defaulting to the IEnumerable<Animal>.

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)

Sep 5 '08 #4
To clarify - it *would* be able to pick a better candidate if the
difference was between Foo<Tand Bar<Twhere Foo<T: Bar<T>, but
this is very different to SomeType<Foovs SomeType<Barwhere Foo :
Bar.
Don't get me started on variance ;-)

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Sep 5 '08 #5
Well, I'm explicit asking for the enumerable object of IEnumerable<Tiger>.

By adding the "Tiger"? yes.

Interestingly (maybe) this is a rare example of where knowing *less*
about a variable is better:

TigerCollection tigersOrig = new TigerCollection();
IEnumerable<Tigertigers = tigersOrig;
....
var t1 = from t in tigers select t;

Now the compiler doesn't have choices - it only knows about
IEnumerable<Tiger>, so it works.

Marc
Sep 5 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Doug Dew | last post by:
This won't compile: using IEnumerable<T> = System.Collections.Generic.IEnumerable<T>; namespace MyNamespace { public class MyClass<T> : IEnumerable<T> { // Appropriate stuff here }
10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
6
by: nicolas.rolland | last post by:
Would anyone know the reson why IList<Tdoes not implements IList ?? This results in strange behaviours, like typeof(IList).IsAssignableFrom(typeof(List<string>)) --true...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
1
by: =?Utf-8?B?RnJhbmNvaXNWaWxqb2Vu?= | last post by:
Hi there, Does anybody know how to return the results of an IEnumerable<typeas an array of the same type i.e type in a web service call without first having to collect all elements in the...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
2
by: Morgan Cheng | last post by:
In .Net 2.0, Generics are introduced. I found that IEnumerable<T> inherites from IEnumerable. This makes implementation of IEnumerable<Thas to have two GetEnumerator methods defined( one for...
0
by: Marc Gravell | last post by:
You could bypass the List<T>'s tendency to use ICollection<Tby simply not giving it an ICollection<T- for example, with the C# 3 extension method below you could use: List<Tlist = new...
2
by: Berryl Hesh | last post by:
Converting in the other direction , IEnumerable<Interfaceto List<ImplInterface>, I can just create a new List<ImplInterface(data) where data is IEnumerable<Interface>. How can I convert the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.