C# question...
So I''m learning C# and I''ve implemented the IEnumerable interface in a simple collection class. But I''m a little worried about how robust it is. When I use foreach with my collections there is an implicit downcast from an ''object'' and I don''t get any compiler errors when I accidentally use the wrong type in the foreach statement.
foreach (string s in MyCollection)
that compiles fine even if MyCollection''s ''Current'' property doesn''t return a string.
Any tips on how to make it more robust or is it not worth worrying about? Or am I just doing something wrong?
"C combines all the power of assembly language with all the ease of use of assembly language"
It should throw a bad cast exception. All System.Collections.* do this. Even though a compile-time warning is preferable, an exception is better than nothing.
I think I might use foreach sparingly then and just use a normal for loop instead. Sound sensible?
[edited by - NotAnAnonymousPoster on December 13, 2002 9:58:38 AM]
[edited by - NotAnAnonymousPoster on December 13, 2002 9:59:13 AM]
[edited by - NotAnAnonymousPoster on December 13, 2002 9:58:38 AM]
[edited by - NotAnAnonymousPoster on December 13, 2002 9:59:13 AM]
"C combines all the power of assembly language with all the ease of use of assembly language"
Actually I suppose more subtle bugs could appear in the for loop anyway. I''ve just realised that I quite like the foreach statement.
"C combines all the power of assembly language with all the ease of use of assembly language"
Jeffrey Richter has a pretty good article on implementing IEnumerable: http://www.codeguru.com/cs_collections/enumeration.html
Generally you should only do an explicit implementation of IEnumerable and expose as public a GetEnumerator method which returns a strongly typed IEnumerator implementation. This at least will prevent any unboxing and accompanying performance implications.
For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski
Generally you should only do an explicit implementation of IEnumerable and expose as public a GetEnumerator method which returns a strongly typed IEnumerator implementation. This at least will prevent any unboxing and accompanying performance implications.
For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
I am very impressed with that. I hadn't thought about the performance issues with structures but I am happy enough that the compiler now tells me when I'm using the wrong object type.
EDIT: Oh yeah and I've learned something else from that article about what foreach actually does.
That article's just made me realise how that comment was a load of bollocks as well. Hadn't realised the difference between accessing elements with subscripts and using enumerations. And I didn't know what foreach actually expanded to.
[edited by - NotAnAnonymousPoster on December 14, 2002 6:49:33 AM]
EDIT: Oh yeah and I've learned something else from that article about what foreach actually does.
quote: ]Original post by me
I think I might use foreach sparingly then and just use a normal for loop instead. Sound sensible?
That article's just made me realise how that comment was a load of bollocks as well. Hadn't realised the difference between accessing elements with subscripts and using enumerations. And I didn't know what foreach actually expanded to.
[edited by - NotAnAnonymousPoster on December 14, 2002 6:49:33 AM]
"C combines all the power of assembly language with all the ease of use of assembly language"
With the Everett (MSVS 7.1) edition of C# you could get a compiler error in this case if you use a strongly-typed collection (using generics).
Read more: Microsoft Announces New Capabilities for C#, C++ [^]
Read more: Microsoft Announces New Capabilities for C#, C++ [^]
quote:
New Visual C# Features
Generics:Similar to the concept of C++ templates, generics let developers create strongly-typed collections, such as an ArrayList of integers, more easily. Currently, the base collection classes that ship with the framework accept Object types—which allow developers to write their own strongly-typed collection classes by inheriting from the abstract CollectionBase, NameValueCollectionBase, and DictionaryBase classes. However, doing so requires writing explicit code, both to identify each object type stored in the collection and, when extracting values from the collection, to cast the stored Object variable back into the appropriate type.
In contrast, generics are built into the language; in fact, says Sridharan, they''re built into the next version of the framework itself, and thus accessible to any language that wants to use them. Sridharan said "surfacing" the feature in any particular language requires a certain amount of code; it''s not automatic.
C# is the first .NET language to make the new feature available. It''s unclear whether Microsoft will update other languages to implement the feature, although from the number of newsgroup requests about creating strongly-typed collections in VB.NET, one might reasonably expect that the VB.NET team is at least considering it.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Generics aren''t scheduled for Everett, unfortunately. Generics and the other changes to C# won''t be available until VS.NET For Yukon is released.
For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski
For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Oh darn, forgot that... :|
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
December 19, 2002 01:39 PM
Was just strolling through the forums and happened across this post, even though it''s old, I thought I''d post this in case it helps someone ... I wrote a little script that generates a strongly typed collection for you.
http://www.codecube.net/item.asp?cc_ItemID=140
http://www.codecube.net/item.asp?cc_ItemID=140
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement