Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Collection classes - yes or no

I'm a relative newbie to C#, although I am a competent programmer, and I confess that I am totally confused as to whether or not it is a good idea to write custom collection classes. So many people seem to say "don't", yet there is a whole set of base classes for it in C#.

Here is my specific case. I have a timetable application. As part of that, I have a service class, and the service class contains collections of things service-y, such as route links. A route link is itself a custom class:

public class Service
{
  public RouteLinks RL;    // A collection of RouteLink types
  ...
}

public class RouteLink
{
    public string FirstStopRef;
    public string LastStopRef;
    public Tracks RouteTrack;    // Another collection, this time of Track types
}

So far I have looked at using Dictionary as the type for RouteLinks, because I need to be able to reference them. This is fine in principle. However, the process of adding a RouteLink to the RouteLinks collection involves checking to see whether it is already there, or whether it extends and existing route link, or... And for that, I need a custom Add function.

So why is is such bad practice to create custom collection classes? Why shouldn't I just inherit CollectionBase or DictionaryBase?

I should perhaps add that I am transferring this code from VBA [please don't shoot me :)] and there I HAD to implement custom collections.

like image 572
StuartR143 Avatar asked Mar 10 '14 10:03

StuartR143


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Instead of having RouteLinks be a collection type, an easy solution would be to just define another class, let's say RouteLinksRepository. This class will contain a List<RouteLink> and the AddRoute(RouteLink) functionality as well as any other custom logic for interacting with this collection of RouteLink objects. Your service class will then just contain an instance of this repository class.

public class Service
{
  public RouteLinksRepository RL;    // A collection of RouteLink types
  // ...
}

public class RouteLinksRepository
{
    public List<RouteLink> RouteLinks;
    public bool AddRoute(RouteLink linkToAdd)
    {
        //Custom logic on whether or not to add link
    }
    //Your other logic for the class

}

public class RouteLink
{
    public string FirstStopRef;
    public string LastStopRef;
    public Tracks RouteTrack;    // Another collection, this time of Track types
}
like image 55
Daniel Simpkins Avatar answered Sep 30 '22 04:09

Daniel Simpkins