Partially implement the interface

I asked something like this, but still I have no clear idea. My goal is to partially implement the interface in C #.

Is it possible? Is there any pattern to achieve this result?

+3
source share
5 answers

The interface defines the contract. You must fulfill this contract by fulfilling all of its members if you want to use it.

Perhaps using an abstract class will work best for you, so you can define some default behavior by allowing overrides where you need it.

+13
source

NotImplementedException , , NotSupportedException , .

, .NET Framework , NotSupportedException, Stream .

MSDN NotSupportedException:

, , , , , .

+8

, ( , , NotSupportedExceptions)

( SOLID priciples, ) , , ,

+2
source

yes, you can partially implement the interface if you use an abstract class something like this:

public interface myinterface
{
     void a();
     void b();
}
public abstract  class myclass : myinterface
{
    public void a()
    {
        ///do something
    }

  public   abstract void  b(); // keep this abstract and then implement it in child class
}
+2
source

Like other posts, throwing an exception in addition to hiding a member is your best bet.

interface IPartial
{
    void A();
    void B();
}

class Partial : IPartial
{
    public void A()
    {
        // Implementation here
    }
    void IPartial.B()
    {
        throw new NotImplementedException();
    }
}

class Main
{
    Main()
    {
        Partial t = new Partial();
        t.A();
        t.B(); // Compiler error

        IPartial s = new Partial();
        s.A();
        s.B(); // Runtime error
    }
}
0
source

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


All Articles