C # Inheritance: implements + extends

Is it possible to do something like this in C# :

 public class MyClass implements ClassA extends ClassB { } 

I need this because: I have two classes, one of which is Interface , which I will implement in my class, but I would also like to use methods from another class that do some things that I would like to use in my class.

+8
inheritance c # oop
source share
4 answers

C # does not support multiple inheritance . You can get one class and use interfaces for other needs.

Syntax:

 class MyClass : Foo, IFoo, IBar { } interface IFoo { } interface IBar { } class Foo { } 
+16
source share

Try the following:

 using System; public interface A { void DoSmth(); } public class B { public void OpA() { } public void OpB() { } } public class ClassC : B, A { public void DoSmth(){} } 

Remember that you cannot inherit two classes at a particular class level, it can be only one class and any number of interfaces.

+5
source share
  class Base { } interface I1 { } interface I2 { } class Derived : Base, I1, I2 { } static void Main(String[] args) { Derived d = new Derived(); } 
+3
source share

It should look like this:

 public class MyClass: ClassB, InterfaceA{ } 

Class B is the base class.

An interface is an interface.

You can extend only one base class, but you can implement many interfaces.

0
source share

All Articles