Interfaces in C#

In the preceding example, we illustrated how we can declare a base class with some member variables and have them inherited in a derived class. However, there could be some scenarios when we need to have a class inherited from two different classes. Moreover, if we are using a struct, we will not be able to inherit from another struct or class.

Unfortunately, using inheritance, we will not be able to achieve this in a C# application due to the following reasons:

  • Multiple inheritance is not allowed in C#.
  • A struct data type in C# cannot inherit from other structs or class types.

In such scenarios, interfaces come in handy. An interface defines a set of related methods, attributes which each class implementing the interface ...

Get Programming in C#: Exam 70-483 (MCSD) Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.