Polymorphism in C#

Do you have a programming interview question that trips you up sometimes? One of mine is polymorphism in C# during a discussion of Object Oriented Programming (OOP). While I know the answer and use the technique in my programs, I stumble around with this one often, trying to articulate what it means in the context of C#. So, to help me, and you, get this embedded this in our minds, I decided to write this article on this topic.

The definition of polymorphism is when different types of objects use the method or properties with the same name. Therefore, the programmer doesn’t need to know the exact type of an object while coding the application. The precise behavior of a method or property is determined at run-time. In the context of C# and .NET, this primarily means interfaces. Another form of polymorphism in C# is operator overloading, for example, where the + operator can be used to add numbers or concatenate strings. But, for our discussion here we’ll primarily discuss interfaces.

C# Interfaces

While some other languages use other methods to provide polymorphism, C#’s single-inheritance design makes interfaces the way to accomplish this technique. Remember that unlike an abstract class, an interface only defines the behavior, it doesn’t implement it.

Here’s a simple example of what an interface in C# looks like…

public interface ITestItem
{
    int TestId { get; set; }

    string TestDescription { get; set; }

    string RunTest(int testNumber);
}

In this interface, we’re defining two properties and one method. To implement this interface in a class, we would need to indicate that we’re using the interface and provide an implementation for the routines defined in the interface, as seen here…

public class TestGroup : ITestItem
{
    DateTime _testDateTime;

    public TestGroup()
    {
        //load without a parameter
    }

    public TestGroup(string testinfo)
    {
        //load with a parameter
    }

    public int TestId { get; set; }

    public string TestDescription { get; set; }

    public DateTime TimeStamp
    {
        get{return _testDateTime;}
    }

    public string RunTest(int testNumber)
    {

        //run our tests
        _testDateTime = DateTime.Now;
        return null;
    }
}

To show how polymorphism would work, here’s a different class that implements the same interface but has different code…

public class TestUser : ITestItem
{
    int _userId;

    public TestUser(int UserId)
    {
        _userId = UserId;
        //load with a parameter
    }

    public int TestId
    {
        get{return _userId;}
        set{}
    }

    public string TestDescription { get; set; }

    public string RunTest(int testNumber)
    {
        //do our tests
        return null;
    }
}

Also, in C#, you can only inherit from one base class, including abstract classes, but you can use multiple interfaces as shown in this code snippet that adds a IList interface to our class that’s already using the ITestItem interface we defined…

public class TestUser : ITestItem, IList<string>

C# Polymorphism Confusion

A common bit of confusion when it comes to polymorphism in C# is overloading and overriding. These aren’t the same as polymorphism but are sometimes confused with it. Overloading is when there are multiple definitions, also known as signatures, for the same method name within a class. Overriding occurs when a subclass replaces a parent’s implementation of a method with it’s own code. I’ve seen this used as a trick question in a C# interview so watch out for it, especially if you’re interviewing with someone who’s a stickler of OOP academia.

Anyway, I hope this article has helped you understand more about polymorphism in C#.


Next Page »
Theme Provided By Free HTML Layouts