HighTechTalks DotNet Forums  

Generic Collections

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Generic Collections in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Scott M.
 
Posts: n/a

Default Generic Collections - 11-27-2007 , 02:33 PM






Given that in .NET 1.x, we could make a strongly-typed collection by
creating a class that inherited from CollectionBase and implementing custom
Add, Remove, and Item members that only work for a given type and that now,
with Generics we don't need to do that extra work, does it mean that to
fully implement a strongly typed Generic collection in .NET 2.0, I need
nothing more than something like this:

Public Class foo(Of T)
Inherits Generic.List(Of T)
End Class

Since the Generic.List type has Add, Remove, and Item (as well as the Find
methods and other useful collection members) and accessing any particular
item in the collection will return a strong type association to the
underlying object, then what else would I need?


Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Generic Collections - 11-27-2007 , 03:47 PM






Scott M. <smar (AT) nospam (DOT) nospam> wrote:
Quote:
Given that in .NET 1.x, we could make a strongly-typed collection by
creating a class that inherited from CollectionBase and implementing custom
Add, Remove, and Item members that only work for a given type and that now,
with Generics we don't need to do that extra work, does it mean that to
fully implement a strongly typed Generic collection in .NET 2.0, I need
nothing more than something like this:

Public Class foo(Of T)
Inherits Generic.List(Of T)
End Class
You don't even need that. Just use List(Of T) directly in your code -
unless you're actually adding any functionality to it, why bother
creating a new class to derive from it?

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #3  
Old   
Scott M.
 
Posts: n/a

Default Re: Generic Collections - 11-27-2007 , 04:50 PM



Ok thanks.


"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote

Quote:
Scott M. <smar (AT) nospam (DOT) nospam> wrote:
Given that in .NET 1.x, we could make a strongly-typed collection by
creating a class that inherited from CollectionBase and implementing
custom
Add, Remove, and Item members that only work for a given type and that
now,
with Generics we don't need to do that extra work, does it mean that to
fully implement a strongly typed Generic collection in .NET 2.0, I need
nothing more than something like this:

Public Class foo(Of T)
Inherits Generic.List(Of T)
End Class

You don't even need that. Just use List(Of T) directly in your code -
unless you're actually adding any functionality to it, why bother
creating a new class to derive from it?

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #4  
Old   
Barry Kelly
 
Posts: n/a

Default Re: Generic Collections - 11-30-2007 , 06:48 PM



Scott M. wrote:

Quote:
Given that in .NET 1.x, we could make a strongly-typed collection by
creating a class that inherited from CollectionBase and implementing custom
Add, Remove, and Item members that only work for a given type and that now,
with Generics we don't need to do that extra work, does it mean that to
fully implement a strongly typed Generic collection in .NET 2.0, I need
nothing more than something like this:

Public Class foo(Of T)
Inherits Generic.List(Of T)
End Class

Since the Generic.List type has Add, Remove, and Item (as well as the Find
methods and other useful collection members) and accessing any particular
item in the collection will return a strong type association to the
underlying object, then what else would I need?
Don't forget Collection(Of T) base class. It has virtual methods to
notify you of changes to the collection, should you be interested in
them, which can be a reason to override.

-- Barry

--
http://barrkel.blogspot.com/


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.