C# Interfaces Illustrated


This entry is part 7 of 8 in the series C# Interfaces

The book Illustrated C# 7, Fifth Edition by Daniel Solis and Cal Schrotenboer published by Apress says: “An interface is a reference type that specifies a set of function members but does not implement them. That’s left to classes and structs that implement the interface.”

First, imagine that you had a bunch of classes in your program that stored data about people. For example, you had Employees, Customers, Members, Clients and so on. Each class was a little different from each other class. For example, one class has FirstName and LastName, whereas the other class has FName and LName and a third class has FullName.

Now suppose you have a very useful method called PrintInfo(). This method takes in a class and formats some of the data and prints it. To simplify things we’ll just display some data on the console. Now we’ll have a look at the code in the above mentioned book and I will modify it a bit. Here we are able to pass our first class CA to the method PrintInfo() in Program.

Using an interface to make the method PrintInfo usable by any number of classes

using System;
namespace Illustrated
{
    class CA
    {
        public string Name;
        public int Age;
    }
    class CB
    {
        public string First;
        public string Last;
        public double PersonsAge;
    }
    class Program
    {
        static void PrintInfo(CA item)
        {
            Console.WriteLine($"Name: { item.Name }, Age: { item.Age }");
        }
        static void Main()
        {
            CA a = new CA() { Name = "John Doe", Age = 35 };
            PrintInfo(a);
        }
    }
}

Method PrintInfo works great as long as you pass it objects of type CA, but it won’t work if you pass it an object of type CB. That is because of the naming of the internal fields being different. What if we could create classes in such a way that they could be successfully passed into PrintInfo, and PrintInfo would be able to process them regardless of the structure of the class? Interfaces make this possible. Interfaces give us a standard naming convention. Both objects implement the same naming convention. They use the same method names: GetName() and GetAge().

using System;
namespace IllustratedInterfaces { 
    interface IInfo
    {
        string GetName();
        string GetAge();
    }
    class CA : IInfo
    {   // declare that CA implements the interface
        public string Name;
        public int Age;
        // implement the two interface methods:
        public string GetName() { return Name; }
        public string GetAge() { return Age.ToString(); }
    }
    class CB : IInfo
    {   // declare that CB implements the interface
        public string First;
        public string Last;
        public double PersonsAge;
        public string GetName() { return First + " " + Last; }
        public string GetAge() { return PersonsAge.ToString(); }
    }
    class Program
    {
        static void PrintInfo(IInfo item) // pass objects as references to the interface
        {
            Console.WriteLine("Name: {0}     Age: {1}",  item.GetName() , item.GetAge() );
        }
        static void Main()
        {
            // instantiate using object initialization syntax
            CA a = new CA() { Name = "John Doe", Age = 35 };
            CB b = new CB() { First = "Jane", Last = "Smith", PersonsAge = 44.0 };
            // references to the objects are automatically converted to references
            // to the interfaces they implement (in the code below)
            PrintInfo(a);
            PrintInfo(b);

        }
    }
}
Series Navigation<< C# Interfaces Extensibility 3C# Interface IComparable >>