Home

Creating and Using a 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 implementation. 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);

            Response.Write("--------------------------------");
            Response.Write("Employee #:    {0}", staff.EmployeeNumber);
            Response.Write("First Name:    {0}", staff.FirstName);
            Response.Write("Last Name:     {0}", staff.LastName);
            Response.Write("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;

        Response.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);

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

    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;

        Response.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);

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

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

        shapes = exo.GetShapes();

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

        return 0;
    }
}

Covariance and Contravariance

Introduction

Object casting consists of converting an object from one type to another. Traditionally, in computer programming, the casting is done between a variable of one class with a variable declared from a class derived from the first class. 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]; }
}

// A class named Employee
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);
    }
}

// A class named Manager derived from Employee
public class Manager : Employee
{
    public string Title { get; set; }

    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}\n" +
                             "Title:         {4}\n", EmployeeNumber, FirstName,
                             LastName, HourlySalary, Title);
    }
}

public class Exercise
{
    public static int Main()
    {
        Manager mgr = new Manager();
        mgr.EmployeeNumber = 249730;
        mgr.FirstName = "Alex";
        mgr.LastName = "Joyner";
        mgr.HourlySalary = 32.85;
        mgr.Title = "Regional Manager";

        Response.Write(mgr.ToString());

        // Casting a Manager object to an Employee
        Employee empl = (Employee)mgr;

        Response.Write(empl);
        Response.Write("================================");

        return 0;
    }
}

This would produce:

================================
Manager Information
--------------------------------
Employee #:    249730
First Name:    Alex
Last Name:     Joyner
Hourly Salary: 32.85
Title:         Regional Manager
================================
Employee Record
--------------------------------
Employee #:    249730
First Name:    Alex
Last Name:     Joyner
Hourly Salary: 32.85
================================
Press any key to continue . . .

On the surface, casting appears as an easy operation. After all, it should be obvious to compare a son to his father. In computer programming, the operation is traditionally denied in most languages to compare a parent to a child. The following code will cause an error:

public class Exercise
{
    public static int Main()
    {
        Employee empl = null;

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

        Response.Write(empl);

        // Casting an Employee object to a Manager
        Manager mgr = (Manager)empl;

        Response.Write(mgr);
        Response.Write("================================");

        return 0;
    }
}

This would produce:

================================
Employee Record
--------------------------------
Employee #:    253055
First Name:    Joseph
Last Name:     Denison
Hourly Salary: 12.85
================================

Unhandled Exception: System.InvalidCastException: Unable to cast object of type
'Employee' to type 'Manager'.
   at Exercise.Main() in C:\Temporary Projects\Exercise.cs:line 77
Press any key to continue . . .

The error is based on the fact that a parent class has no way of identifying what child it has, because at the time a class is created, it is not aware of child classes that would be derived from it. This operation is still not directly allowed.

Covariance

Remember that you can create a list of objects using a generic collection class. Here are examples:

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]; }
}

// A class named Employee
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);
    }
}

// A class named Manager derived from Employee
public class Manager : Employee
{
    public string Title { get; set; }

    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}\n" +
                             "Title:         {4}\n", EmployeeNumber, FirstName,
                             LastName, HourlySalary, Title);
    }
}

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

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

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

        empl = new Employee();
        empl.EmployeeNumber = 970044;
        empl.FirstName = "James";
        empl.LastName = "Macies";
        empl.HourlySalary = 14.25;
        staff.Add(empl);

        Response.Write("=--= Employees =--=");
        for(int i = 0; i < staff.Count; i++)
            Response.Write(staff.Get(i));

        IPersons<Manager> leaders = new People<Manager>();
        Manager mgr = null;

        mgr = new Manager();
        mgr.EmployeeNumber = 249730;
        mgr.FirstName = "Alex";
        mgr.LastName = "Joyner";
        mgr.HourlySalary = 32.85;
        mgr.Title = "Regional Manager";
        leaders.Add(mgr);

        mgr = new Manager();
        mgr.EmployeeNumber = 579047;
        mgr.FirstName = "Robert";
        mgr.LastName = "Farley";
        mgr.HourlySalary = 28.55;
        mgr.Title = "Shift Supervisor";
        leaders.Add(mgr);

        Response.Write("=--= Managers =--=");
        for (int i = 0; i < leaders.Count; i++)
            Response.Write(leaders.Get(i));

        Response.Write("================================");

        return 0;
    }
}

This would produce:

=--= Employees =--=
================================
Employee Record
--------------------------------
Employee #:    253055
First Name:    Joseph
Last Name:     Denison
Hourly Salary: 12.85
================================
Employee Record
--------------------------------
Employee #:    204085
First Name:    Raymond
Last Name:     Ramirez
Hourly Salary: 9.95
================================
Employee Record
--------------------------------
Employee #:    970044
First Name:    James
Last Name:     Macies
Hourly Salary: 14.25
=--= Managers =--=
================================
Manager Information
--------------------------------
Employee #:    249730
First Name:    Alex
Last Name:     Joyner
Hourly Salary: 32.85
Title:         Regional Manager

================================
Manager Information
--------------------------------
Employee #:    579047
First Name:    Robert
Last Name:     Farley
Hourly Salary: 28.55
Title:         Shift Supervisor

================================
Press any key to continue . . .

Since we have already learned how to return a generic interface from a method, if you want, instead of creating the records locally, you can define a method that generates them, and then return an interface from that method. Here are examples:

using System;

. . . No Change

public class Exercise
{
    private static IPersons<Employee> GetEmployees()
    {
        IPersons<Employee> staff = new People<Employee>();
        Employee empl = null;

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

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

        empl = new Employee();
        empl.EmployeeNumber = 970044;
        empl.FirstName = "James";
        empl.LastName = "Macies";
        empl.HourlySalary = 14.25;
        staff.Add(empl);

        return staff;
    }

    private static IPersons<Manager> GetManagers()
    {
        IPersons<Manager> leaders = new People<Manager>();
        Manager mgr = null;

        mgr = new Manager();
        mgr.EmployeeNumber = 249730;
        mgr.FirstName = "Alex";
        mgr.LastName = "Joyner";
        mgr.HourlySalary = 32.85;
        mgr.Title = "Regional Manager";
        leaders.Add(mgr);

        mgr = new Manager();
        mgr.EmployeeNumber = 579047;
        mgr.FirstName = "Robert";
        mgr.LastName = "Farley";
        mgr.HourlySalary = 28.55;
        mgr.Title = "Shift Supervisor";
        leaders.Add(mgr);

        return leaders;
    }

    public static int Main()
    {
        IPersons<Employee> staff = GetEmployees();

        Response.Write("=--= Employees =--=");
        for (int i = 0; i < staff.Count; i++)
            Response.Write(staff.Get(i));

        IPersons<Manager> leaders = GetManagers();

        Response.Write("=--= Managers =--=");
        for (int i = 0; i < leaders.Count; i++)
            Response.Write(leaders.Get(i));

        Response.Write("================================");

        return 0;
    }
}

When creating objects from your generic collection class, you may be interested in casting one list from one type to a list of another type. Consider the following example:

public class Exercise
{
    private static IPersons<Employee> GetEmployees()
    {
        IPersons<Employee> staff = new People<Employee>();
 
        . . . No Change

        return staff;
    }

    private static IPersons<Manager> GetManagers()
    {
        IPersons<Manager> leaders = new People<Manager>();
 
        . . . No Change

        return leaders;
    }

    public static int Main()
    {
        ICounter<Manager> leaders = GetManagers();
        ICounter<Employee> staff = (ICounter<Employee>)leaders;

        Response.Write("=--= Employees =--=");
        for (int i = 0; i < staff.Count; i++)
            Response.Write(staff.Get(i));


        Response.Write("=--= Managers =--=");
        for (int i = 0; i < leaders.Count; i++)
            Response.Write(leaders.Get(i));

        Response.Write("================================");

        return 0;
    }
}

This would produce the following error:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type
'People`1[Manager]' to type 'ICounter`1[Employee]'.
   at Exercise.Main() in C:\Exercise1\Exercise.cs:line 143
Press any key to continue . . .

In realty, you can manage casting only among compatible objects. That is, you can perform casting from objects of classes that implement the same interface but there are rules you must follow.

If you want value casting to be performed among the objects of its implementers, when creating the generic interface, precede the type parameter with the out keyword. Here is an example:

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

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

 After doing this, the casting would work. Here is an example:

public class Exercise
{
    private static IPersons<Employee> GetEmployees()
    {
        IPersons<Employee> staff = new People<Employee>();
 
        . . . No Change

        return staff;
    }

    private static IPersons<Manager> GetManagers()
    {
        IPersons<Manager> leaders = new People<Manager>();
 
        . . . No Change

        return leaders;
    }

    public static int Main()
    {
        ICounter<Manager> leaders = GetManagers();
        ICounter<Employee> staff = (ICounter<Employee>)leaders;

        Response.Write("=--= Employees =--=");
        for (int i = 0; i < staff.Count; i++)
            Response.Write(staff.Get(i));


        Response.Write("=--= Managers =--=");
        for (int i = 0; i < leaders.Count; i++)
            Response.Write(leaders.Get(i));

        Response.Write("================================");

        return 0;
    }
}

In this cast, we performed the cast by preceding the the variable with the name of the interface and the other class. Actually, you can omit it:

using System;

public interface ICounter<out 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);
    }
}

// A class named Manager derived from Employee
public class Manager : Employee
{
    public string Title { get; set; }

    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}\n" +
                             "Title:         {4}\n", EmployeeNumber, FirstName,
                             LastName, HourlySalary, Title);
    }
}

public class Exercise
{
    private static IPersons<Employee> GetEmployees()
    {
        IPersons<Employee> staff = new People<Employee>();
        Employee empl = null;

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

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

        empl = new Employee();
        empl.EmployeeNumber = 970044;
        empl.FirstName = "James";
        empl.LastName = "Macies";
        empl.HourlySalary = 14.25;
        staff.Add(empl);

        return staff;
    }

    private static IPersons<Manager> GetManagers()
    {
        IPersons<Manager> leaders = new People<Manager>();
        Manager mgr = null;

        mgr = new Manager();
        mgr.EmployeeNumber = 249730;
        mgr.FirstName = "Alex";
        mgr.LastName = "Joyner";
        mgr.HourlySalary = 32.85;
        mgr.Title = "Regional Manager";
        leaders.Add(mgr);

        mgr = new Manager();
        mgr.EmployeeNumber = 579047;
        mgr.FirstName = "Robert";
        mgr.LastName = "Farley";
        mgr.HourlySalary = 28.55;
        mgr.Title = "Shift Supervisor";
        leaders.Add(mgr);

        return leaders;
    }

    public static int Main()
    {
        ICounter<Manager> leaders = GetManagers();
        ICounter<Employee> staff = leaders;

        Response.Write("=--= Employees =--=");
        for (int i = 0; i < staff.Count; i++)
            Response.Write(staff.Get(i));


        Response.Write("=--= Managers =--=");
        for (int i = 0; i < leaders.Count; i++)
            Response.Write(leaders.Get(i));

        Response.Write("================================");

        return 0;
    }
}

This technique of managing value casting is called covariance.

Introduction to Built-In Covariance Interfaces

Covariance is a technique used in collection classes and makes it possible to cast one collection indirectly to another, using their common parent interface. To assist you with covariance, the .NET Framework provides some already created classes you can use in your applications. They are:

  • IEnumerable: This interface is primarily used to enumerate the members of a collection. Of course, it must be a generic collection class that implements this interface. An example would be the List<> class. 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 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);
        }
    }
    
    // A class named Manager derived from Employee
    public class Manager : Employee
    {
        public string Title { get; set; }
    
        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}\n" +
                                 "Title:         {4}\n", EmployeeNumber, FirstName,
                                 LastName, HourlySalary, Title);
        }
    }
    
    public class Exercise
    {
        private static List<Employee> GetEmployees()
        {
            List<Employee> staff = new List<Employee>();
            Employee empl = null;
    
            empl = new Employee();
            empl.EmployeeNumber = 253055;
            empl.FirstName = "Joseph";
            empl.LastName = "Denison";
            empl.HourlySalary = 12.85;
            staff.Add(empl);
    
            empl = new Employee();
            empl.EmployeeNumber = 204085;
            empl.FirstName = "Raymond";
            empl.LastName = "Ramirez";
            empl.HourlySalary = 9.95;
            staff.Add(empl);
    
            empl = new Employee();
            empl.EmployeeNumber = 970044;
            empl.FirstName = "James";
            empl.LastName = "Macies";
            empl.HourlySalary = 14.25;
            staff.Add(empl);
    
            return staff;
        }
    
        private static List<Manager> GetManagers()
        {
            List<Manager> leaders = new List<Manager>();
            Manager mgr = null;
    
            mgr = new Manager();
            mgr.EmployeeNumber = 249730;
            mgr.FirstName = "Alex";
            mgr.LastName = "Joyner";
            mgr.HourlySalary = 32.85;
            mgr.Title = "Regional Manager";
            leaders.Add(mgr);
    
            mgr = new Manager();
            mgr.EmployeeNumber = 579047;
            mgr.FirstName = "Robert";
            mgr.LastName = "Farley";
            mgr.HourlySalary = 28.55;
            mgr.Title = "Shift Supervisor";
            leaders.Add(mgr);
    
            return leaders;
        }
    
        public static int Main()
        {
            IEnumerable<Manager> managers = GetManagers();
            IEnumerable<Employee> employees = managers;
    
            foreach (Employee empl in employees)
                Response.Write(empl);
    
            foreach (Manager man in managers)
                Response.Write(man);
    
            Response.Write("================================");
    
            return 0;
        }
    }

    This would produce:

    ================================
    Manager Information
    --------------------------------
    Employee #:    249730
    First Name:    Alex
    Last Name:     Joyner
    Hourly Salary: 32.85
    Title:         Regional Manager
    
    ================================
    Manager Information
    --------------------------------
    Employee #:    579047
    First Name:    Robert
    Last Name:     Farley
    Hourly Salary: 28.55
    Title:         Shift Supervisor
    
    ================================
    Manager Information
    --------------------------------
    Employee #:    249730
    First Name:    Alex
    Last Name:     Joyner
    Hourly Salary: 32.85
    Title:         Regional Manager
    
    ================================
    Manager Information
    --------------------------------
    Employee #:    579047
    First Name:    Robert
    Last Name:     Farley
    Hourly Salary: 28.55
    Title:         Shift Supervisor
    
    ================================
    Press any key to continue . . .
  • IEnumerator: This interface is used to iterate through a collection that uses memory and must release it after use
  • IQueryable: This interface is used to perform queries in a LINQ collection
  • IGrouping: This interface also is used in LINQ queries to group members that respond to a criterion

Contravariance

Contra-variance, also named contravariance, is the opposite to covariance. Instead of out, it uses the in keyword and makes it possible to cast a collection to the parent variable.

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));

        Response.Write("=---= Original List =---=");
        foreach (Employee staff in employees)
            Response.Write(staff);

        employees.InsertRange(1, contractors);

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

        return 0;
    }
}

Previous Copyright © 2010-2015, FunctionX Home