Solved

Am I using Generics the right way?

Posted on 2006-11-15
5
176 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Iterate a dictionnary to change values 4 53
XML & .net 5 41
ADO.NET ENTITY DATA MODEL 3 32
User Authentication using Digital Certificate 2 27
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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now