Can I omit interface methods in abstract classes in C #?

I am a Java developer who is trying to move to C #, and I am trying to find a good equivalent to some Java code. In Java, I can do this:

public interface MyInterface
{
    public void theMethod();
}

public abstract class MyAbstractClass implements MyInterface
{
    /* No interface implementation, because it abstract */
}

public class MyClass extends MyAbstractClass
{
    public void theMethod()
    {
        /* Implement missing interface methods in this class. */
    }
}

What is the C # equivalent? The best solutions using abstract / new / override, etc., It seems to lead to the fact that "theMethod" is declared the body of one form or another of the abstract class. How can I eliminate the reference to this method in an abstract class, where it does not belong, and ensure its implementation in a specific class?

+3
source share
2 answers

You cannot, you must do it like this:

public interface MyInterface 
{ 
    void theMethod(); 
} 

public abstract class MyAbstractClass : MyInterface 
{ 
     public abstract void theMethod();
} 

public class MyClass : MyAbstractClass 
{ 
    public override void theMethod() 
    { 
        /* Implement missing interface methods in this class. */ 
    } 
} 
+5
source

, , .

.

public interface MyInterface
{
     void theMethod();
}

public abstract class MyAbstractClass: MyInterface
{
     public abstract void theMethod();
}

public class MyClass: MyAbstractClass
{
     public override void theMethod()
     {
          /* implementation */
     }
}
+2

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


All Articles