This is going to be where most of the magic happens. Now that we've seen the objects we will be collecting, let's move on to how we will be collecting them. This class is equally trivial, and contains only a FirstName and LastName property, as well as a constructor to initialize those properties immediately, and a default parameterless constructor. Person's Last Name public string LastName Person' First Name public string FirstName Paramaterized constructor for immediate instantiation public Person( string first, string last) As for right now, we are only interested in the way it is derived by other classes because we are going to use this to limit the types that can be used with our generic collection. Even though the base class we are using here is trivial, much functionality could be added to the base class for a Business Object. The class contains only one property called UniqueId ( Guid), and a default constructor which initializes that property. This is the base class from which all of our other business objects will be derived. The example source code attached to this article contains two classes which make up a simple Business Logic Layer that we will use with our generic collection class. Example Business Logic Layerīefore we actually write our collection class, we are going to write some foundation code with which it can be used. For now, let's define the objects we are going to be collecting. We must also implement two methods which are required by IEnumerator and IEnumerator the interfaces which ICollection extends to allow foreach enumeration. Public interface ICollection : IEnumerable, IEnumerableĪs you can see, in order to extend ICollection in our generic collection, we must implement two properties and five methods. A Closer Look at ICollectionīefore we get started, let's take a look at how ICollection is defined in the. Even though C++ is more flexible in this area, the C# model provides a decreased amount of runtime errors by limiting what can be done based on Generic Constraints. These are only a few of the differences at a glance, but it is important to take note of them if you are coming from a familiar C++ frame of reference. One of the main differences between C++ Template Classes and C# Generics is that in C#, you cannot use arithmetic operators (except custom operators) in a generic class.Ĭ# does not allow type parameters to have default types, nor does C# allow you to use the type parameter ( T) as the base class for the Generic type. What is the Difference Between C++ Templates and C# Generics? If you are a C++ fan, you will realize that Generics operate in a very similar way to Template Classes in C++, but with a few differences. Using Generics significantly decreases the amount of run-time errors due to casting or boxing/unboxing because the types used in a generic operations are evaluated at compile time. They provide a type-safe method of accessing data used in collections and/or function calls. Generics were first introduced into the C# language in. This article attempts to present a solid example of how to implement ICollection and IEnumerator in order to create a generic, type-safe, and expandable collection class. I took what I could learn from all of the information I found, and came up with what I believe to be a decent, functioning implementation of ICollection as well as IEnumerator to enable enumeration through a foreach statement. Surely, I thought, there must be someone who implements ICollection.Īfter a bit of searching around, I found a few examples showing incomplete and/or non-functioning code on how to implement this interface. I set out looking for examples on the Internet, and found that decent examples of this implementation were few and far between, and some sites actually stated that it wasn't worth looking at because no one implements ICollection. I thought the best way to go about this would be to write a generic class that implemented the generic interface ICollection. It seemed to me that a generic collection class which would be enumerable (i.e., able to be traversed using a foreach statement) wouldn't be very difficult to implement in C#. As the need for more and more business objects arose, I immediately stopped what I was doing and started looking for a more dynamic solution.Īs I could recall, C++ had something called template classes that would accept a dynamic variable type. I had already written a non-generic collection class (one for each of my business objects, sadly) which inherited from CollectionBase. In the process of building three-tier web applications, I came across the need for a generic collection class which would be able to hold a type-safe collection of any of my business objects from my Business Logic Layer. Download source code and demo project - 33.6 KB.
0 Comments
Leave a Reply. |