Can someone explain a few things about collections to me.
In C# 2 generics, you can create a collection class by inheriting from
System.Collecti ons.ObjectModel .Collection. Using this you can iterate through
the collection and use "foreach" on the collection.
public class Cars : System.Collecti ons.ObjectModel .Collection<Car { }
So, could someone explain to me why you would want to create a GENERIC
custom collection class.
See http://www.codeproject.com/dotnet/Ge...ustomClass.asp -- "Writing
Custom Collection Classes" section
I haven't really used collection classes or had to write one for .Net 1.1,
so i am a little perplexed.
Many thanks, CR 7 5748
Well, as a thought, you might want to abstract the implementation from
the public API - especially if your code is being consumed by external
parties. That way, you can change the internals without breaking the
clients - for instance, switching to a dictionary of some kind.
This is also necessary if the base container is sealed (or doesn't
provide many virtuals), and you want to customize the behaviour - i.e.
extra methods (can't do if sealed) or extra checks (can't do if not
virtual). You can also limit the methods available - perhaps Clear()
isn't sensible for your scenario?
Marc
thanks marc,
so for the most part, using the System.Collecti ons.ObjectModel .Collection
will be good enough.
On Jun 4, 10:01 am, CodeRazor <CodeRa...@disc ussions.microso ft.com>
wrote:
so for the most part, using the System.Collecti ons.ObjectModel .Collection
will be good enough.
Well, unless you need to add extra functionality, I'd normally use the
collections in System.Collecti ons.Generic, personally.
Jon
I've looked at the article... the author has a generic collection
inheriting from CollectionBase - so isn't exactly good gode to follow
;-p Plus a good half-dozen other glaring mistakes... (new ToString(),
GENERICTYPE vs GenericType [won't build], Item[int] instead of
this[int], etc - plus common style/naming conventions). IMHO, not a
good article.
Stick to Collection<Tor List<Tand you'll be fine. When you need
something more complex, you'll know ;-p
Marc
Note that you do not need to inherit from Collection in order to implement
the "foreach" construct. Just write a method that returns IEnumerable<T>,
as in
public IEnumerable<TAs tar()
{
}
which I implemented in a class called Graph.
This allows you to use the "yield return" construct (in Astar) so that
callers can invoke the "foreach" function on the method, as in
foreach (KSnode ksn in graph.Astar())
{
}
This makes Astar look like a collection to your users, without your needing
to inherit from collection (or even making use of List<T>, etc).
For other examples of using generics in combination with the "foreach"
construct, in order to implement collection-like behavior, see http://www.frontiernet.net/~fredm/dps/Contents.htm .
"CodeRazor" <Co*******@disc ussions.microso ft.comwrote in message
news:D1******** *************** ***********@mic rosoft.com...
Can someone explain a few things about collections to me.
In C# 2 generics, you can create a collection class by inheriting from
System.Collecti ons.ObjectModel .Collection. Using this you can iterate
through
the collection and use "foreach" on the collection.
public class Cars : System.Collecti ons.ObjectModel .Collection<Car { }
So, could someone explain to me why you would want to create a GENERIC
custom collection class.
See http://www.codeproject.com/dotnet/Ge...ustomClass.asp -- "Writing
Custom Collection Classes" section
I haven't really used collection classes or had to write one for .Net 1.1,
so i am a little perplexed.
Many thanks, CR
The advantage of generics is that you generally don't need custom
collection classes any more. In .NET 1.1 people used to create custom
collections so that they could enforce a constraint on the type of the
contents of the collection. With Generics in .NET 2.0 you no longer
need a custom class for this.
Most of the time you'll just use List<Tor Dictionary<TKey , TValue>
directly instead of creating a custom collection.
The exception is when you need different behavior. That is a far
rarer situation than simply different contents. One example of
different behavior from my current application is a custom KeySet<T>
collection. This is basically a dictionary without a value, just keys
as I've found myself often needing to use a dictionary to store a
unique set of keys and checking existence in the collection without
caring about any actual value. Implementing this as a custom generic
class made usage of the class cleaner than when using
IDictionary<T,T >.
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Mon, 4 Jun 2007 01:16:03 -0700, CodeRazor
<Co*******@disc ussions.microso ft.comwrote:
>Can someone explain a few things about collections to me.
In C# 2 generics, you can create a collection class by inheriting from System.Collect ions.ObjectMode l.Collection. Using this you can iterate through the collection and use "foreach" on the collection.
public class Cars : System.Collecti ons.ObjectModel .Collection<Car { }
So, could someone explain to me why you would want to create a GENERIC custom collection class.
See http://www.codeproject.com/dotnet/Ge...ustomClass.asp -- "Writing Custom Collection Classes" section
I haven't really used collection classes or had to write one for .Net 1.1, so i am a little perplexed.
Many thanks, CR
thanks for your help.
it was useful. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jeff Stewart |
last post by:
Let's say I create a collection of DateTime objects like so:
Dim clxn_Times As Collection = New Collection()
Let's go on to say I add 3 elements to the collection. How do I pull off
the result represented by the following?
clxn_Times(0) = clxn_Times(0).AddDays(-1)
I can't find a way to edit an item in place. But not knowing very much
|
by: Kiran A K |
last post by:
hi,
suppose in my application i need to perform a lot of
insertions and deletions on my data structure.
the ideal data structure in this scenario would be a linked list.
unlike java, c# does not have a built-in "LinkedList" class.
so which collection class should i use in C#?
regards,
kiran
|
by: Radu |
last post by:
Hi,
I have VS.NET 2002 and I'm reading about ReadOnlyCollectionBase, Collection,
and DictionaryBase. In the following reference regarding VB2005, I can't
find them:
http://www.dotnet2themax.com/dotnetbrowser/ShowNamespace.aspx?asm=System&ns=System.Collections.Generic
http://www.dotnet2themax.com/dotnetbrowser/ShowNamespace.aspx?asm=System&ns=System.Collections.Specialized
1. Have they been dropped ?
|
by: Jeff S. |
last post by:
In a Windows Forms application I plan to have a collection of structs - each
of which contains a bunch of properties describing a person (e.g., LastName,
FirstName, EmployeeID, HomeAddress, ZipCode, etc). So each instance of the
struct will describe a person - and about 900 instances (people) will be
contained in the collection.
Users must be able to search for a specific person by any of the properties
(e.g., LastName, FirstName,...
|
by: Robert Schneider |
last post by:
Hi,
the msdn documentation provides an Dinosaurs example that uses the
objectmodel.collection class. Just have a look on the help page for this
class.
I don't understand what happens here. Or I cannot find more detailed
documentation for this behavior. What I mean is the invocation of the
Dinosaurs.Add method which is derived from Collection(of String). The method
seems to invoke the overridden InsertItem method. Can this method be...
| |
by: cwineman |
last post by:
Hello,
I'm hoping to do something using Generics, but I'm not sure it's possible.
Let's say I want to have a bunch of business objects and a data access class
cooresponding to each business object. For each business object, I would
have something like below:
public class Apple
{
//apple stuff
|
by: rh1200la |
last post by:
Hey all. Here's my question. I'm building a Shipping rates component
with the following classes:
Package which contains basic package information. I also have shipper
classes that implement an interface I built. They have basic shipper
information to their respective API's so I will have 3 different
Shipper classes: UPS, FedEx, and USPS. I would like the Package class
to contain a collection of generic shipper objects. I think...
|
by: Mike P |
last post by:
I'm pretty new to OOP and I'm just starting looking at generics and
collection classes. I understand why I might want to create a business
class, but can somebody please explain to me the reasons behind creating
collection classes?
Many thanks,
Mike
|
by: manwood |
last post by:
I have a generic method that performs some basic sorting and removal of duplicates on some classes, but I cannot get the parameter to pass into the Distinct function working.
This is my class code that implements IEqualityComparer<T>:
public class Artist : IEqualityComparer<Artist>
{
public Artist()
{
}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |