How Would You Describe An Interface?



A mini challenge for you… how would you describe an interface in C#? What could you use it for?

Any other talk of questions? Please create a new post.

unlisted #2

listed #3


I could use “If” for an if statement, such as; if (this == true) return;


Not sure I follow.


Interface is a characteristic to be given to the class. Regular class Inheritance is what the object is and Interface inheritance is an addition to what it can do. For example, the whole IPointer interface “family” is there to allow the class to receive further information from interactions with the cursor, it’s an additional characteristic but isn’t what define the object. Another example is IDamageable, usually created in order to handle damage across multiple different objects.


To keep it simple, it is a contract. Even though the syntax is similar, it is not an inheritance. A class can implement an interface, meaning that is has to implement the functions with the signature that the interface requires it to implement. It can be a very powerful thing to use when you want to avoid inheritance and still assure that certain functionalities are given on a range of classes. The specific definition of the contracted functions can vary across all the classes that implement the same interface, while type, name and parameters will be the same. Therefore it can be great to use to reduce strict dependencies. If I have a function that requires a certain object but I don’t know yet what class that specific object is I can simply use an interface to ship around this problem.


I was attemptingToBeClever but I failed :frowning:

you wrote that as a second question but I believe you may have meant “it”.


I’ve corrected it so now no one will understand :wink:


I would say that IDamageable is part of what the class that implements it does. It’s a way of segregating the responsibilities I guess. However, it can never have functionality which makes it less like a characteristic. In other languages, you can use multiple inheritance or mixins for this.


Yes, I guess that it’s more accurate to say that it’s a way of segragating the responsabilities rather than a characteristic.