top of page

How To Utilize Interfaces In C#

An “Interface” is similar to an “Abstract Class” in the sense that they can both force implementations. While an Abstract Class can be thought of as a partial template, Interfaces can be looked at as more of a contract, in the sense that whatever is in that contract MUST be implemented.

Interfaces do NOT allow any implementations and can not contain fields. You are only able to use methods and properties.

Interfaces allow for an important concept in Object Oriented Programming known as “Polymorphism” which means “Many Forms”. is where an object shares “Relevancy”.

To create your interface you will start by creating a new C# script. I’m going to name mine “IDamageable”

[Pro Tip]

As with anything, when creating a new interface, it is important to take “Best Practices” into account.

The standard naming convention for an interface script starts with a capital “I” to signify that it is an interface, followed by a name that typically ends with “able” such as “IDamagable”, “IFixable”, “IHealable”, etc.

This is not a requirement, but it is good to keep up with practices like this as they are developed through experience over time within the industry in order to create better, smoother, and more organized workflows.

To declare an interface, follow the gif example below. This is not a class, and will NOT be inheriting from Monobehavior.


Now if you were to try and add a variable such as the one in the example here, you will see that it throws an error. That is because interfaces can NOT contain fields.

So what are you supposed to do?

Well, even though interfaces can’t use fields, they CAN use “Properties”.


Public Interface IDamageable
{
    int Health { get; get; }
}

“Properties” are named members of classes, structures, and interfaces. “Member Variables” are called “Fields”. Properties are an extension of fields and are accessed using the same syntax.

You are also able to use “Methods” within an interface. But you can NOT use any implementation details.



public interface IDamageable
{
    void Damage();
}

You CAN declare “Parameters” within your methods though! As seen in the example below

public interface IDamageable
{
    void Damage (int damageAmount)
{

Now that you have your interface built, it’s time to implement it! You will be doing that through your other scripts. For this example, I will be creating another C# script and I will name it “Enemy”.

To implement your interface, simply type a comma ( , ) followed by a space and then your interface’s name.

public class Enemy : Monobehavior, IDamageable
{
}


Now you’ll notice that there is an error being thrown for the interface. That is because the “property” of type “Health” and the “Damage” “method” with the “parameter” of type “int” that we declared within the interface has not been implemented within this script yet. When inheriting from an interface, you HAVE to implement its declared features.


public interface IDamageable
{
    int Health { get; get; }
    void Damage (int damageAmount)
}

public class Enemy : Monobehavior, IDamageable
{
    public int Health { get; get; }
    public void Damage (int damageAmount)
    {
    }
}

And there it is! You can now access and utilize your interface! In the example below I add an implementation to the “Damage” method within this enemy script.

public class Enemy : Monobehavior , IDamageable
{
    public int Health { get; get; }
    public void Damage (int damageAmount)
    {
        Health => damageAmount;
    }
}

If I wanted to, I could call the interface from any number of other enemies, NPCs, or whatever and modularly change it to fit each character accordingly!


public class Enemy1 : Monobehavior, IDamageable
{
    public int Health { get; get; }
    public void Damage (int DamageAmount)
    {
        Health -+ damageAmoubt * 2;
    }
}

[Pro Tip]

You can even access and implement MULTIPLE different interfaces to a single Monobehavior class. Just remember that ALL of the interface’s requirements MUST be met each of them that is added.


public class Enemy2 : Monobehavior, IDamageable, IHealable, IDestructable
{
    public int Health { get; get; }
    
    public void Damage(int damageAmount)
    {
        Health -+ damageAmount * 2;
    }
    
    public void Healing()
    {
    }
    
    public void Destroy()
}



Source: Medium - Adam Reed


The Tech Platform

0 comments

Recent Posts

See All
bottom of page