Is it possible to define an interface in C# which has a default implementation? (so that we can define a class implementing that interface without implementing that particular default method).
I know extension methods (as explained in this link for example). But that is not my answer because having a method extension like the following, the compiler still complains about implementing MyMethod in MyClass:
public interface IMyInterface { string MyMethod(); } public static class IMyInterfaceExtens { public static string MyMethod(this IMyInterface someObj) { return "Default method!"; } } public class MyClass: IMyInterface { // I want to have a default implementation of "MyMethod" // so that I can skip implementing it here }
I am asking this because (at least as far as I understand) it is possible to do so in Java (see here).
PS: having an abstract base class with some method is also not my answer simply because we don't have multiple inheritance in C# and it is different from having a default implementation for interfaces (if possible!).
Default implementations help with that. An interface member can now be specified with a code body, and if an implementing class or struct does not provide an implementation of that member, no error occurs. Instead, the default implementation is used.
Default interface methods enable an API author to add methods to an interface in future versions without breaking source or binary compatibility with existing implementations of that interface. The feature enables C# to interoperate with APIs targeting Android (Java) and iOS (Swift), which support similar features.
Default methods enable you to add new functionality to existing interfaces and ensure binary compatibility with code written for older versions of those interfaces. In particular, default methods enable you to add methods that accept lambda expressions as parameters to existing interfaces.
C# Modifiers in InterfacesBy default, the default methods of an interface are virtual. If you want then you can also make them sealed and private by using the sealed or private modifier. Similarly, if you are not providing implementation to interface methods, then by default they are going to be abstract.
I develop games so I often want to have common function for all implementations of an interface but at the same time allow each implementation to do its own thing as well, much like a subclass' virtual / override methods would function.
This is how I do it:
public class Example { void Start() { WallE wallE = new WallE(); Robocop robocop = new Robocop(); // Calling Move() (from IRobotHelper) // First it will execute the shared functionality, as specified in IRobotHelper // Then it will execute any implementation-specific functionality, // depending on which class called it. In this case, WallE's OnMove(). wallE.Move(1); // Now if we call the same Move function on a different implementation of IRobot // It will again begin by executing the shared functionality, as specified in IRobotHlper's Move function // And then it will proceed to executing Robocop's OnMove(), for Robocop-specific functionality. robocop.Move(1); // The whole concept is similar to inheritence, but for interfaces. // This structure offers an - admittedly dirty - way of having some of the benefits of a multiple inheritence scheme in C#, using interfaces. } } public interface IRobot { // Fields float speed { get; } float position { get; set; } // Implementation specific functions. // Similar to an override function. void OnMove(float direction); } public static class IRobotHelper { // Common code for all IRobot implementations. // Similar to the body of a virtual function, only it always gets called. public static void Move(this IRobot iRobot, float direction) { // All robots move based on their speed. iRobot.position += iRobot.speed * direction; // Call the ImplementationSpecific function iRobot.OnMove(direction); } } // Pro-Guns robot. public class Robocop : IRobot { public float position { get; set; } public float speed { get; set;} private void Shoot(float direction) { } // Robocop also shoots when he moves public void OnMove(float direction) { Shoot(direction); } } // Hippie robot. public class WallE : IRobot { public float position { get; set; } public float speed { get; set; } // Wall-E is happy just moving around public void OnMove(float direction) { } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With