c sharp

How to Implement Interface Properties in C#

Objected Oriented Programming may seem simplistic and a little intuitive when playing in low level. However, when you are building complex OOP applications, things can become quite complex and easy to clobber.

In an attempt to solve such potential issues, languages such as C# provide OOP features like interfaces.

An interface is use to define a strict contract for OOP classes without specify the behavior. The role of an interface is to describe the capabilities and functionalities that a class provides without actually defining how they should be implemented.

In this tutorial, we will talk about the role and functionalities of the interface properties and how to use them in a C# application.

Interface Property

An interface can include property declarations, methods, events, and indexer declarations. The class that implements the property is then required to provide an implementation for these properties.

This ensures that the class contains a specific set of properties available which can help to ensure that the objects from the class are used in a specific format.

If that doesn’t make sense yet, bear with us as we delve into a more practical example.

Basic Example:

Let us take a simple interface called ICar that declares a property called “EnginePower”. Any class that implements this interface must then provide an implementation for this property. Take the following example code:

public interface ICar
{
    int EnginePower { get; set; }
}

public class Car : ICar
{
    private int power;

    public int EnginePower
    {
        get { return power; }
        set { power = value; }
    }
}

In this example, the ICar interface declares a property called “EnginePower” that has both a getter and setter. This ensures that you can read and write to/from it.

The “Car” class then implements this interface and hence must provide an implementation for the “EnginePower” property.

For our case, the private field power holds the data and the property is implemented with a basic “get” and “set” accessors.

Read-Only Interface Prop

You might also have a class that declares a read-only property. This means that it only has a getter. If you encounter such a property, you must ensure that your class respects this rule. Otherwise, the compiler will complain.

Let us take a Rectangle interface:

public interface IRectangle
{
    double Width { get; }
    double Height { get; }
    double Area { get; }
}

public class Rectangle : IRectangle
{
    public double Width { get; }
    public double Height { get; }
    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }
    public double Area => Width * Height;
}

You will notice that the IRectangle interface has a three read-only properties: Width, Height, and Area.

In that case, when we implement these properties in the “Rectangle” class, we must respect this as demonstrated by the “Area” property.

Conclusion

In this fundamental’s tutorial, we walked through the process of dealing with C# interface properties. We also covered about the read-only properties and how to work with them.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list