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”
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”.
“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.
You CAN declare “Parameters” within your methods though! As seen in the example below
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.
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.
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.
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!
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.