By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,991 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,991 IT Pros & Developers. It's quick & easy.

foreach & generics syntax proposal

P: n/a
since generics allow us to implement several IEnumerable<T> interfaces on a
single class, I think that "foreach" should somehow reflect that.

suppose we have a enumerable class

class C : IEnumerable, IEnumerable<int>, IEnumerable<string> { ...

we are allowed to enumerate IEnumerable:

foreach ( object item in o ) ...

but other IEnumerables are not so easy:

foreach ( int item in (IEnumerable<int>)o ) ...

or

class C : ....
{
public IEnumerable<int> Ints
{
get
{
return (IEnumerable<int>)this;
....

foreach ( int item in o.Ints ) ...

What I think of is some form of selecting the IEnumerable with foreach:

foreach ( object item in o ) // enumerate IEnumerable
foreach ( int item in o ) // enumerate IEnumerable
foreach ( string item in o ) // enumerate IEnumerable
foreach<int> ( int item in o ) // enumerate IEnumerable<int>
foreach<string> ( string item in o ) // enumerate IEnumerable<string>

since the type name is duplicated in former cases, I would even like to
write:

....
foreach<int> ( item in o ) ... // enumerate IEnumerable<int>
foreach<string> ( item in o ) ... // enumerate IEnumerable<string>

and that's it. I open the discussion.

Regards,
Wiktor Zychla

Mar 30 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Wiktor Zychla [C# MVP]" <wz*****@nospm.ii.uni.wroc.pl.nospm> wrote in
message news:u%******************@tk2msftngp13.phx.gbl...
since generics allow us to implement several IEnumerable<T> interfaces on
a single class, I think that "foreach" should somehow reflect that.
Implementing IEnumerable<int> should mean that your class IS-A sequence of
int which means that it is also a sequence of object but it cannot logically
also be a sequence of string hence implementing IEnumerable<string> is
logically wrong.
suppose we have a enumerable class

class C : IEnumerable, IEnumerable<int>, IEnumerable<string> { ...

we are allowed to enumerate IEnumerable:

foreach ( object item in o ) ...

but other IEnumerables are not so easy:

foreach ( int item in (IEnumerable<int>)o ) ...

or

class C : ....
{
public IEnumerable<int> Ints
{
get
{
return (IEnumerable<int>)this;


Now there IS a valid use for enumerable properties or methods because then
the class HAS-A sequence of whatever.
The text book example (although not with different types) is:

class MyTreeNode
{

// enumerate nodes in PreOrder
public IEnumerable<MyTreeNode> PreOrder()
{
....
yield return n; // these things are really easy in 2.0 - in
1.1 it is too much like work.
....
}
public IEnumerable<MyTreeNode> InOrder()
{
....
}
public IEnumerable<MyTreeNode> PostOrder()
{
....
}
public IEnumerable<MyTreeNode> Children
{
...
}
}

I tend to feel that methods are more appropriate than properties for this
sort of stuff however you could see how something like node.Children and
person.Parents are more naturally a properties.
Mar 30 '06 #2

P: n/a
Well wouldn't that allow a logic error like:

foreach<int> ( string item in o )

Why make the user type the type twice? Why make the user need to know that
the object is generic?

And let's say that you pass a collection of widgets to a client, the
client's method need only accept an IEnumerable and foreach across it,
accessing the widgets. He needn't know the structure of the collection any
more than that. If you then make your collection generic, will he need to
change his code to reflect that?
Mar 30 '06 #3

P: n/a
Wiktor,

Frankly I don't see
foreach<int> ( int item in o ) // enumerate IEnumerable<int>
or even
foreach<int> ( item in o ) ... // enumerate IEnumerable<int>
as a significant improvement over
foreach ( int item in (IEnumerable<int>)o ) ...


This last one doesn't require that much more typing, and it's pretty
clear what's happening. So I don't think it's work introducing new
syntax for this.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.