Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring Interface method implementation in C#

Tags:

c#

oop

Suppose an Interface I has two methods. For example Method1() and Method2().

A class A Implements an Interface I.

Is it possible for class A to implement only Method1() and ignore Method2()?

I know as per rule class A has to write implementation of both methods. I am asking if there any way to violate this rule?

like image 436
Mazhar Khan Avatar asked Nov 28 '22 02:11

Mazhar Khan


1 Answers

You can avoid implementing it (a valid scenario) but not ignore it altogether (a questionable scenario).

public interface IFoo
{
    void A();
    void B();
}

// This abstract class doesn't know what to do with B(), so it puts
// the onus on subclasses to perform the implementation.
public abstract class Bar : IFoo
{
    public void A() { }
    public abstract void B();
}
like image 87
Tim M. Avatar answered Dec 05 '22 01:12

Tim M.