Showing posts with label Creating C# Generic Interface. Show all posts
Showing posts with label Creating C# Generic Interface. Show all posts

Tuesday, 21 February 2012

Abstract Class versus Interface

Abstract Class versus Interface

 

Introduction

In this article along with the demo project I will discuss Interfaces versus Abstract classes. The concept of Abstract classes and Interfaces is a bit confusing for beginners of Object Oriented programming. Therefore, I am trying to discuss the theoretical aspects of both the concepts and compare their usage. And finally I will demonstrate how to use them with C#.

Background

An Abstract class without any implementation just looks like an Interface; however there are lot of differences than similarities between an Abstract class and an Interface. Let's explain both concepts and compare their similarities and differences.

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

Both Together

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.
When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:
Feature
Interface
Abstract class
Multiple inheritance
A class may inherit several interfaces.
A class may inherit only one abstract class.
Default implementation
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties
Core VS Peripheral
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity
If various implementations only share method signatures then it is better to use Interfaces.
If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Speed
Requires more time to find the actual method in the corresponding classes.
Fast
Adding functionality (Versioning)
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and Constants No fields can be defined in interfaces An abstract class can have fields and constrants defined

Using the Code

Let me explain the code to make it a bit easier. There is an Employee abstract class and an IEmployee interface. Within the Abstract class and the Interface entity I am commenting on the differences between the artifacts.
I am testing both the Abstract class and the Interface by implementing objects from them. From the Employee abstract class, we have inherited one object: Emp_Fulltime. Similarly from IEmployee we have inherited one object: Emp_Fulltime2.
In the test code under the GUI, I am creating instances of both Emp_Fulltime and Emp_Fulltime2 and then setting their attributes and finally calling the calculateWage method of the objects.

Abstract Class Employee

 

using System;

namespace AbstractsANDInterfaces
{
    /// 

    /// Summary description for Employee.
    /// 
    
    public abstract class Employee
    {
        //we can have fields and properties 

        //in the Abstract class
        protected String id;
        protected String lname;
        protected String fname;

        //properties

        public abstract String ID
        {
            get;
            set;
        }

        public abstract String FirstName
        {
            get;
            set;
        }
        
        public abstract String LastName
        {
            get;
            set;
        }
        //completed methods

        public String Update()
        {
            return "Employee " + id + " " + 
                      lname + " " + fname + 
                      " updated";
        }
        //completed methods

        public String Add()
        {
            return "Employee " + id + " " + 
                      lname + " " + fname + 
                      " added";
        }
        //completed methods

        public String Delete()
        {
            return "Employee " + id + " " + 
                      lname + " " + fname + 
                      " deleted";
        }
        //completed methods

        public String Search()
        {
            return "Employee " + id + " " + 
                      lname + " " + fname + 
                      " found";
        }

        //abstract method that is different 

        //from Fulltime and Contractor
        //therefore i keep it uncompleted and 
        //let each implementation 
        //complete it the way they calculate the wage.

        public abstract String CalculateWage();
        
    }


}
 
 
 
 
 

Interface Employee

 

 

using System;


namespace AbstractsANDInterfaces
{
    /// <summary>

    /// Summary description for IEmployee.
    /// </summary>
    public interface IEmployee
    {
        //cannot have fields. uncommenting 

        //will raise error!        //        protected String id;        //        protected String lname;        //        protected String fname;

        //just signature of the properties 
        //and methods.
        //setting a rule or contract to be 
        //followed by implementations.

        String ID
        {
            get;
            set;
        }

        String FirstName
        {
            get;
            set;
        }
        
        String LastName
        {
            get;
            set;
        }
        
        // cannot have implementation

        // cannot have modifiers public 
        // etc all are assumed public
        // cannot have virtual

        String Update();

        String Add();

        String Delete();

        String Search();

        String CalculateWage();
    }
}

 

Inherited Objects

Emp_Fulltime:

using System;

namespace AbstractsANDInterfaces
{
    /// 

    /// Summary description for Emp_Fulltime.
    /// 
     
    //Inheriting from the Abstract class
    public class Emp_Fulltime : Employee
    {
        //uses all the properties of the 

        //Abstract class therefore no 
        //properties or fields here!

        public Emp_Fulltime()
        {
        }


        public override String ID
        {
            get

            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        
        public override String FirstName
        {
            get

            {
                return fname;
            }
            set
            {
                fname = value;
            }
        }

        public override String LastName
        {
            get

            {
                return lname;
            }
            set
            {
                lname = value;
            }
        }

        //common methods that are 
        //implemented in the abstract class
        public new String Add()
        {
            return base.Add();
        }
        //common methods that are implemented 

        //in the abstract class
        public new String Delete()
        {
            return base.Delete();
        }
        //common methods that are implemented 

        //in the abstract class
        public new String Search()
        {
            return base.Search();
        }
        //common methods that are implemented 

        //in the abstract class
        public new String Update()
        {
            return base.Update();
        }
        
        //abstract method that is different 

        //from Fulltime and Contractor
        //therefore I override it here.
        public override String CalculateWage()
        {
            return "Full time employee " + 
                  base.fname + " is calculated " + 
                  "using the Abstract class...";
        }
    }
}
 
 
 
Emp_Fulltime2:
 
 
 
using System;

namespace AbstractsANDInterfaces
{
    /// 
    /// Summary description for Emp_fulltime2.

    /// 
    
    //Implementing the interface
    public class Emp_fulltime2 : IEmployee
    {
        //All the properties and 

        //fields are defined here!
        protected String id;
        protected String lname;
        protected String fname;

        public Emp_fulltime2()
        {
            //


            // TODO: Add constructor logic here
            //

        }

        public String ID
        {
            get

            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        
        public String FirstName
        {
            get
            {
                return fname;
            }
            set

            {
                fname = value;
            }
        }

        public String LastName
        {
            get
            {
                return lname;
            }
            set
            {
                lname = value;
            }
        }

        //all the manipulations including Add,Delete, 

        //Search, Update, Calculate are done
        //within the object as there are not 
        //implementation in the Interface entity.
        public String Add()
        {
            return "Fulltime Employee " + 
                          fname + " added.";
        }

        public String Delete()
        {
            return "Fulltime Employee " + 
                        fname + " deleted.";
        }

        public String Search()
        {
            return "Fulltime Employee " + 
                       fname + " searched.";
        }

        public String Update()
        {
            return "Fulltime Employee " + 
                        fname + " updated.";
        }
        
        //if you change to Calculatewage(). 

        //Just small 'w' it will raise 
        //error as in interface
        //it is CalculateWage() with capital 'W'.
        public String CalculateWage()
        {
            return "Full time employee " + 
                  fname + " caluculated using " + 
                  "Interface.";
        }
    }
} 


Code for Testing

 

//This is the sub that tests both 
//implementations using Interface and Abstract
private void InterfaceExample_Click(object sender, 
                                System.EventArgs e)
{
    try

    {

        IEmployee emp;

        Emp_fulltime2 emp1 = new Emp_fulltime2();

        emp =  emp1;
        emp.ID = "2234";
        emp.FirstName= "Rahman" ;
        emp.LastName = "Mahmoodi" ;
        //call add method od the object

        MessageBox.Show(emp.Add().ToString());
        
        //call the CalculateWage method
        MessageBox.Show(emp.CalculateWage().ToString());


    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}

private void cmdAbstractExample_Click(object sender, 
                                   System.EventArgs e)
{

    Employee emp;

    emp = new Emp_Fulltime();
    

    emp.ID = "2244";
    emp.FirstName= "Maria" ;
    emp.LastName = "Robinlius" ;
    MessageBox.Show(emp.Add().ToString());

    //call the CalculateWage method

    MessageBox.Show(emp.CalculateWage().ToString());

}
 

Conclusion

In the above examples, I have explained the differences between an abstract class and an interface. I have also implemented a demo project which uses both abstract class and interface and shows the differences in their implementation.
 

 


 

 

Thursday, 16 February 2012

Creating C# Generic Interface

Creating  C#  Generic Interface




A Custom Generic Interface

 
Introduction


We now know how rich the .NET Framework is with generic classes and interfaces. Still, at times you will want to create your own generic class. You can create it from scratch. You can implement one of the .NET Framework built-in interfaces. Or you can just create your own generic collection class.
 
Creating a Generic Interface

There is nothing magical with creating a generic interface. You must primarily follow the rules of creating an interface except that you must add a parameter type. Here is an example:
public interface ICounter<T>
{
}
You should also add the members that the implementers will have to override. Here is an example:
public interface ICounter<T>
{
    int Count { get; }
    T Get(int index);
}

In the same way, you can derive a generic interface from another generic interface. Here is an example:
public interface ICounter<T>
{
    int Count { get; }
    T Get(int index);
}

public interface IPersons<T> : ICounter<T>
{
    void Add(T item);
}
Implementing a Generic Interface

After creating the generic interface, when deriving a class from it, follow the formula we reviewed for inheriting from a generic class. Here is an example:
public interface ICounter<T>
{
    int Count { get; }
    T Get(int index);
}

public interface IPersons<T> : ICounter<T>
{
    void Add(T item);
}

public class People<T> : IPersons<T>
{
    
}
When implementing the derived class, you must observe all rules that apply to interface derivation. That is, you must implement all the members of the generic interface. Of course, you can also add new members if you want. Here is an example:
public interface ICounter<T>
{
    int Count { get; }
    T Get(int index);
}

public interface IPersons<T> : ICounter<T>
{
    void Add(T item);
}

public class People<T> : IPersons<T>
{
    private int size;
    private T[] persons;

    public People()
    {
        size = 0;
        persons = new T[10];
    }

    public int Count { get { return size; } }

    public void Add(T pers)
    {
        persons[size] = pers;
        size++;
    }

    public T Get(int index) { return persons[index]; }
}
After implementing the interface, you can declare a variable of the class and use it as you see fit. Here is an example:
using System;

public interface ICounter<T>
{
    int Count { get; }
    T Get(int index);
}

public interface IPersons<T> : ICounter<T>
{
    void Add(T item);
}

public class People<T> : IPersons<T>
{
    private int size;
    private T[] persons;

    public People()
    {
        size = 0;
        persons = new T[10];
    }

    public int Count { get { return size; } }

    public void Add(T pers)
    {
        persons[size] = pers;
        size++;
    }

    public T Get(int index) { return persons[index]; }
}

public class Employee
{
    public long EmployeeNumber { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double HourlySalary { get; set; }

    public Employee(long number = 0, string fName = "John",
                    string lName = "Doe", double salary = 12.05D)
    {
        EmployeeNumber = number;
        FirstName = fName;
        LastName = lName;
        HourlySalary = salary;
    }

    public override string ToString()
    {
        base.ToString();

        return string.Format("================================\n" +
                             "Employee Record\n" +
                             "--------------------------------\n" +
                             "Employee #:    {0}\nFirst Name:    {1}\n" +
                             "Last Name:     {2}\nHourly Salary: {3}",
                             EmployeeNumber, FirstName,
                             LastName, HourlySalary);
    }
}

public class Exercise
{
    public static int Main()
    {
        IPersons<Employee> employees = new People<Employee>();

        Employee empl = null;

        empl = new Employee();
        empl.EmployeeNumber = 253055;
        empl.FirstName = "Joseph";
        empl.LastName = "Denison";
        empl.HourlySalary = 12.85;
        employees.Add(empl);

        empl = new Employee();
        empl.EmployeeNumber = 204085;
        empl.FirstName = "Raymond";
        empl.LastName = "Ramirez";
        empl.HourlySalary = 9.95;
        employees.Add(empl);

        empl = new Employee();
        empl.EmployeeNumber = 970044;
        empl.FirstName = "Christian";
        empl.LastName = "Riley";
        empl.HourlySalary = 14.25;
        employees.Add(empl);

        for (int i = 0; i < employees.Count; i++)
        {
            Employee staff = employees.Get(i);

            Console.WriteLine("--------------------------------");
            Console.WriteLine("Employee #:    {0}", staff.EmployeeNumber);
            Console.WriteLine("First Name:    {0}", staff.FirstName);
            Console.WriteLine("Last Name:     {0}", staff.LastName);
            Console.WriteLine("Hourly Salary: {0}", staff.HourlySalary);
        }

        return 0;
    }
}
This would produce:
--------------------------------
Employee #:    253055
First Name:    Joseph
Last Name:     Denison
Hourly Salary: 12.85
--------------------------------
Employee #:    204085
First Name:    Raymond
Last Name:     Ramirez
Hourly Salary: 9.95
--------------------------------
Employee #:    970044
First Name:    Christian
Last Name:     Riley
Hourly Salary: 14.25
Press any key to continue . . .
Passing a Generic Interface as Argument

A generic interface is primarily a normal interface like any other. It can be used to declare a variable but assigned the appropriate class. It can be returned from a method. It can be passed as argument.
You pass a generic interface primarily the same way you would an interface. In the body of the method, you can ignore the argument or use it any way appropriate. For example, you can access its members. Here is an example:
using System;

public interface IShapes<T>
{
    int Count { get; }
    void Add(T item);
    T Get(int index);
}

public class GeometricShapes<T> : IShapes<T>
{
    private int size;
    private T[] items;

    public GeometricShapes()
    {
        size = 0;
        items = new T[10];
    }

    public int Count { get { return size; } }

    public void Add(T item)
    {
        this.items[this.size] = item;
        this.size++;
    }

    public T Get(int index) { return this.items[index]; }
}

public interface IRound
{
    string Name { get; }
    double Radius { get; set; }
    double Diameter { get; }
    double Circumference { get; }
    double Area { get; }
}

public class Circle : IRound
{
    protected double rad;
    protected string id;

    public Circle(double radius = 0.00D)
    {
        this.rad = radius;
    }

    public string Name { get { return "Circle"; } }

    public double Radius
    {
        get { return rad; }
        set
        {
            if (rad <= 0) rad = 0;
            else rad = value;
        }
    }

    public double Diameter { get { return rad * 2; } }

    public double Circumference { get { return rad * 2 * 3.14159; } }

    public double Area { get { return rad * rad * 3.14159; } }
}

public class Exercise
{
    public Circle GetShape()
    {
        double rad = 0.00D;

        Console.Write("Enter the radius: ");
        rad = double.Parse(Console.ReadLine());

        return new Circle(rad);
    }

    public void ShowShapes(IShapes<IRound> shps)
    {
        for (int i = 0; i < shps.Count; i++)
        {
            IRound rnd = shps.Get(i);

            Console.WriteLine("================================");
            Console.WriteLine("{0} Characteristics", rnd.Name);
            Console.WriteLine("--------------------------------");
            Console.WriteLine("Radius:        {0}", rnd.Radius);
            Console.WriteLine("Diameter:      {0}", rnd.Diameter);
            Console.WriteLine("Circumference: {0}", rnd.Circumference);
            Console.WriteLine("Area:          {0}", rnd.Area);
        }
        Console.WriteLine("===============================");
    }

    public static int Main()
    {
        Exercise exo = new Exercise();
        GeometricShapes<IRound> shapes = new GeometricShapes<IRound>();
        
        IRound rnd = exo.GetShape();
        shapes.Add(rnd);
        rnd = exo.GetShape();
        shapes.Add(rnd);
        rnd = exo.GetShape();
        shapes.Add(rnd);
        rnd = exo.GetShape();
        shapes.Add(rnd);
        rnd = exo.GetShape();
        shapes.Add(rnd);

        Console.Clear();
        exo.ShowShapes(shapes);

        return 0;
    }
}
Here is an example of running the application:
Enter the radius: 14.48
Enter the radius: 6.36
Enter the radius: 112.84
Enter the radius: 55.85
Enter the radius: 8.42
...
================================
Circle Characteristics
--------------------------------
Radius:        14.48
Diameter:      28.96
Circumference: 90.9804464
Area:          658.698431936
================================
Circle Characteristics
--------------------------------
Radius:        6.36
Diameter:      12.72
Circumference: 39.9610248
Area:          127.076058864
================================
Circle Characteristics
--------------------------------
Radius:        112.84
Diameter:      225.68
Circumference: 708.9940312
Area:          40001.443240304
================================
Circle Characteristics
--------------------------------
Radius:        55.85
Diameter:      111.7
Circumference: 350.915603
Area:          9799.318213775
================================
Circle Characteristics
--------------------------------
Radius:        8.42
Diameter:      16.84
Circumference: 52.9043756
Area:          222.727421276
===============================
Press any key to continue . . .
Returning a Generic Interface

To indicate that a method must return a generic interface, when creating it, specify its return type as the interface with the appropriate parameter type. Here is an example:
public IShapes<IRound> GetShapes()
{

}
As the number one rule for all methods that return a value, before exiting the method, you must return an object that is compatible with the generic interface. To do this, in the body of the method, you can declare a variable of a class that implements the interface, use that variable any way you wan, and return it. Here is an example:
using System;

public interface IShapes<T>
{
    int Count { get; }
    void Add(T item);
    T Get(int index);
}

public class GeometricShapes<T> : IShapes<T>
{
    private int size;
    private T[] items;

    public GeometricShapes()
    {
        size = 0;
        items = new T[10];
    }

    public int Count { get { return size; } }

    public void Add(T item)
    {
        this.items[this.size] = item;
        this.size++;
    }

    public T Get(int index) { return this.items[index]; }
}

public interface IRound
{
    string Name { get; }
    double Radius { get; set; }
    double Diameter { get; }
    double Circumference { get; }
    double Area { get; }
}

public class Circle : IRound
{
    protected double rad;
    protected string id;

    public Circle(double radius = 0.00D)
    {
        this.rad = radius;
    }

    public string Name { get { return "Circle"; } }

    public double Radius
    {
        get { return rad; }
        set
        {
            if (rad <= 0) rad = 0;
            else rad = value;
        }
    }

    public double Diameter { get { return rad * 2; } }

    public double Circumference { get { return rad * 2 * 3.14159; } }

    public double Area { get { return rad * rad * 3.14159; } }
}

public class Exercise
{
    public Circle GetShape()
    {
        double rad = 0.00D;

        Console.Write("Enter the radius: ");
        rad = double.Parse(Console.ReadLine());

        return new Circle(rad);
    }

    public IShapes<IRound> GetShapes()
    {
        GeometricShapes<IRound> rounds = new GeometricShapes<IRound>();

        IRound rnd = GetShape();
        rounds.Add(rnd);
        rnd = GetShape();
        rounds.Add(rnd);
        rnd = GetShape();
        rounds.Add(rnd);
        rnd = GetShape();
        rounds.Add(rnd);

        return rounds;
    }

    public void ShowShapes(IShapes<IRound> shps)
    {
        for (int i = 0; i < shps.Count; i++)
        {
            IRound rnd = shps.Get(i);

            Console.WriteLine("================================");
            Console.WriteLine("{0} Characteristics", rnd.Name);
            Console.WriteLine("--------------------------------");
            Console.WriteLine("Radius:        {0}", rnd.Radius);
            Console.WriteLine("Diameter:      {0}", rnd.Diameter);
            Console.WriteLine("Circumference: {0}", rnd.Circumference);
            Console.WriteLine("Area:          {0}", rnd.Area);
        }
        Console.WriteLine("===============================");
    }

    public static int Main()
    {
        Exercise exo = new Exercise();
        IShapes<IRound> shapes = new GeometricShapes<IRound>();

        shapes = exo.GetShapes();

        Console.Clear();
        exo.ShowShapes(shapes);

        return 0;
    }
}
Additional Techniques of Using Built-In Interfacces

 
Creating a List From an Existing Collection

In our introduction to the List<> class, we mentioned the default constructor and the constructor that allows you to specify the start amount of memory for a new List variable. The List class is equipped with a third constructor whose syntax is:
public List(IEnumerable<T> collection);
This constructor allows you to create a new list using an existing collection of items. To use it, pass it a list created from a collection class that implements the IEnumerable<> interface. Here is an example:
using System;
using System.Collections.Generic;

public class Employee
{
    public long EmployeeNumber { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double HourlySalary { get; set; }

    public Employee(long number = 0, string fName = "John",
                    string lName = "Doe", double salary = 12.05D)
    {
        EmployeeNumber = number;
        FirstName = fName;
        LastName = lName;
        HourlySalary = salary;
    }
}

public class Records<T> : IEnumerable<T>
{
    private int size;
    private T[] items;

    public Records()
    {
        size = 0;
        items = new T[10];
    }

    public virtual int Count
    {
        get { return size; }
    }

    public void Add(T item)
    {
        this.items[this.size] = item;
        this.size++;
    }

    public T Get(int index) { return items[index]; }

    public IEnumerator<T> GetEnumerator()
    {
        int counter = 0;

        while (counter < Count)
        {
            yield return items[counter];
            counter++;
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        int counter = 0;

        while (counter < Count)
        {
            yield return items[counter];
            counter++;
        }
    }
}

public class Exercise
{
    public static int Main()
    {
        Records<Employee> contractors = new Records<Employee>();

        Employee empl = new Employee(397947, "David", "Redson", 18.75);
        contractors.Add(empl);
        contractors.Add(new Employee(174966, "Alfred", "Swanson", 12.94));
        contractors.Add(new Employee(848024, "Alima", "Bieyrou", 14.05));
        contractors.Add(new Employee(number: 397462, fName: "Robert",
                                     lName: "Nants", salary : 22.15));

        List<Employee> employees = new List<Employee>(contractors);
        
        return 0;
    }
}
Inserting a Range of Value From a Known Collection

In our introduction to the List<> class, we saw how to insert an item at a specific position. The class also allows you to insert not one but a range of values or objects. This operation is handled by the InserRange() method. Its syntax is:
public void InsertRange(int index, IEnumerable<T> collection);
This method takes two arguments. The first specifies the index from where to start adding the new item. The items would come from a class that implements the IEnumerable<> interface. Here is an example of calling this method:
using System;
using System.Collections.Generic;

public class Employee
{
    public long EmployeeNumber { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double HourlySalary { get; set; }

    public Employee(long number = 0, string fName = "John",
                    string lName = "Doe", double salary = 12.05D)
    {
        EmployeeNumber = number;
        FirstName = fName;
        LastName = lName;
        HourlySalary = salary;
    }

    public override string ToString()
    {
        base.ToString();

        return string.Format("================================\n" +
                             "Manager Information\n" +
                             "--------------------------------\n" +
                             "Employee #:    {0}\nFirst Name:    {1}\n" +
                             "Last Name:     {2}\nHourly Salary: {3}",
                             EmployeeNumber, FirstName,
                             LastName, HourlySalary);
    }
}

public class Records<T> : IEnumerable<T>
{
    private int size;
    private T[] items;

    public Records()
    {
        size = 0;
        items = new T[10];
    }

    public virtual int Count
    {
        get { return size; }
    }

    public void Add(T item)
    {
        this.items[this.size] = item;
        this.size++;
    }

    public T Get(int index) { return items[index]; }

    public IEnumerator<T> GetEnumerator()
    {
        int counter = 0;

        while (counter < Count)
        {
            yield return items[counter];
            counter++;
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        int counter = 0;

        while (counter < Count)
        {
            yield return items[counter];
            counter++;
        }
    }
}

public class Exercise
{
    public static int Main()
    {
        Records<Employee> contractors = new Records<Employee>();

        Employee empl = new Employee(397947, "David", "Redson", 18.75);
        contractors.Add(empl);
        contractors.Add(new Employee(174966, "Alfred", "Swanson", 12.94));
        contractors.Add(new Employee(405809, "Amie", "Tripp", 16.55));
        contractors.Add(new Employee(294815, "Theodore", "Ludlum", 8.05));
        contractors.Add(new Employee(848024, "Alima", "Bieyrou", 14.05));
        contractors.Add(new Employee(number: 397462, fName: "Robert",
                                     lName: "Nants", salary : 22.15));

        List<Employee> employees = new List<Employee>();
        employees.Add(new Employee(925741, "Alex", "Woods", 24.85));
        employees.Add(new Employee(248388, "Peter", "Sandt", 20.42));
        employees.Add(new Employee(680284, "David", "Ruphian", 10.42));

        Console.WriteLine("=---= Original List =---=");
        foreach (Employee staff in employees)
            Console.WriteLine(staff);

        employees.InsertRange(1, contractors);

        Console.WriteLine("=---= After inserting new items =---=");
        foreach (Employee staff in employees)
            Console.WriteLine(staff);

        return 0;
    }
}