Link to home
Start Free TrialLog in
Avatar of brdrok
brdrok

asked on

Am I using Generics the right way?

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
}
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of devsolns
devsolns

Yeah I was going to point out, but Alexs already did that your functionality is already in List<T>.
Avatar of brdrok

ASKER

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>
Avatar of brdrok

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial