The default implementation of the method for C # interfaces?

Is it possible to define an interface in C # that has a default implementation? (so that we can define a class that implements this interface without implementing this particular default method).

I know extension methods (as described in this link ). But this is not my answer, because having a method extension similar to the following, the compiler still complains about the implementation of 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 ask about this because (at least as far as I understand) this can be done in Java (see here ).

PS: with an abstract base class with some method, not my answer is also simply because we do not have multiple inheritance in C #, and it differs from the default for interfaces (if possible!).

+13
source share
4 answers

I develop games, so I often want to have a common function for all implementations of the interface, but at the same time it allows each implementation to do its own thing, just like the functions of virtual / redefinition of a subclass.

Here 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 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 Move function // And then it will proceed to executing Robocop 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) { } } 
+12
source

Short answer:

No, you cannot write an implementation of a method in interfaces.

Description:

Interfaces are similar to a contract, so the types that will inherit from it will need to determine the implementation, if you have a script, you need a method with a default implementation, then you can make your class abstract and define a default implementation for the method that you want to.

Example:

 public abstract class MyType { public string MyMethod() { // some implementation } public abstract string SomeMethodWhichDerivedTypeWillImplement(); } 

and now in the Dervied class:

 public class DerivedType : MyType { // now use the default implemented method here } 
+11
source

C # v8 will begin to allow the implementation of a specific method in interfaces. This will allow your specific implementation classes not to interrupt when changing interfaces that will be implemented in the future.

The next version of the language may be something like this:

 interface IA { void NotImplementedMethod(); void M() { WriteLine("IA.M"); } //method definition present in the interface } 

See this GitHub issue # 288 . In addition, Mads Torgersen details this upcoming feature in this 9-channel video.

Note The current version of C # in RTM state is v7 at the time of writing this answer.

+7
source

Not directly, but you can define an extension method for the interface and then implement it somehow like this

 public interface ITestUser { int id { get; set; } string firstName { get; set; } string lastName { get; set; } string FormattedName(); } static class ITestUserHelpers { public static string FormattedNameDefault(this ITestUser user) { return user.lastName + ", " + user.firstName; } } public class TestUser : ITestUser { public int id { get; set; } public string firstName { get; set; } public string lastName { get; set; } public string FormattedName() { return this.FormattedNameDefault(); } } 

Edit * It is important that the extension method and the method you execute are called differently, otherwise you will most likely get stackoverflow.

+1
source

Source: https://habr.com/ru/post/1261512/


All Articles