Solved

Am I using Generics the right way?

Posted on 2006-11-15
5
181 Views
Last Modified: 2010-04-16
Hello,

I have started to play with Generics in C# 2.0.  I know what they are about but still a little unsure on how to use Generics appropriatly.  Below is my first attempt at using Generics.  If I were to use code as show below, am I realizing the benefits of Generics (i.e. type-safety, performance improvement, no need to (un)box, etc)?  

Thanks

public class EmployeeCollection : IEnumerable<Employee>
{
   List<Employee> m_empCollection = new List<Employee>();
       
   public EmployeeCollection()
   {}

    public void Add(Employee newItem)
    {
        m_empCollection.Add(newItem);
     }

    public void Remove(Employee oldItem)
    {
         m_empCollection.Remove(oldItem);
     }

     public Employee this[int index]
     {
         get { return m_empCollection[index]; }
      }

     public int Count
     {
         get { return m_empCollection.Count; }
     }

     #region IEnumerable<Employee> Members
     public IEnumerator<Employee> GetEnumerator()
     {
        return m_empCollection.GetEnumerator();
     }

    #endregion

    #region IEnumerable Members

     IEnumerator IEnumerable.GetEnumerator()
     {
         return m_empCollection.GetEnumerator();
     }

    #endregion
}
0
Comment
Question by:brdrok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17946911
Assuming that Employee is class (reference type):

Type-safety - yes. Using List<Employee> instead of ArrayList ensures type safety.
Performance improvement - minimal. Code working with List<Employee> doesn't need to make runtime casting (like ArrayList requires), this gives some performance improvement.
Unboxing - doesn't apply here, because Employee is reference type.

Assuming that Employee is structure (value type):

Type-safety - yes.
Performance improvement - significant. Code working with List<Employee> doesn't need to make runtime casting (like ArrayList requires) and doesn't need to make boxing. ArrayList requires boxing for value type, because it works with Objects.
Unboxing - using List<Employee> prevents unboxing.

Performance improvement and no unboxing - this also applies for primitive value types, like int, double etc.

Notice that  List<Employee> class itself implements IEnumerable<Employee>, it can do all that your EmployeeCollection class does.
0
 
LVL 13

Expert Comment

by:devsolns
ID: 17949392
Yeah I was going to point out, but Alexs already did that your functionality is already in List<T>.
0
 
LVL 7

Author Comment

by:brdrok
ID: 17949490
Thanks for the very awesome reply.  In this case, Employee would be a reference type.  Eventually something like this will be used in a web application and I figure every little performance gain wouldn't hurt.  

When you wrote:
Notice that  List<Employee> class itself implements IEnumerable<Employee>, it can do all that your EmployeeCollection class does

I am not sure what you mean by that.  Back in the 1.1 days,  I would have something like:
public class EmployeeCol : CollectionBase

the reason being is that the CollectionBase already implements all the goody interfaces (IList, ICollection, IEnumerable)

Are you perhaps suggesting to have the EmployeeCollection class inherit from the Generic.List<....> class? i.e.

public class EmployeeCollection : System.Collections.Generic.List<Employee>
0
 
LVL 7

Author Comment

by:brdrok
ID: 17949512
Perhaps it's a judgement call, but more often than not, I found myself using the indexer quite a bit.  In 1.1., I often times would have something like this:

public Employee this[int index]
{
  get { return ((Employee)(List[index])); }
}

having to type-cast (hopefully I using the right terminology here).  With Generics, I am not dealing with "object" types anymore.  Am I right or off-base?
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 500 total points
ID: 17949717
I mean, unless you add some other functions to EmployeeCollection class, it doesn't have any more functionality than its m_empCollection member. Notice that every public function or property, including indexer, is redirected to exactly same member of m_empCollection.
Suppose I am client of this class. What features does it have which can convince me to use it? I can create List<Employee> myself and have the same functionality. Wrapper only adds operations reducing performance.
However, if you have some other code in this class, this makes sence.

About casting in indexer - you are right. All operations with generic collections are done without casting, this is main generic collections advantage.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question