Home

C# Programming: 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 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;
    }
}
 
 

Home Copyright © 2010-2016, FunctionX