C# Error CS1721 – Class ‘{0}’ cannot have multiple base classes: ‘{1}’ and ‘{2}’

C# Error

CS1721 – Class ‘{0}’ cannot have multiple base classes: ‘{1}’ and ‘{2}’

Reason for the Error & Solution

Class ‘class’ cannot have multiple base classes: ‘class_1’ and ‘class_2’

The most common cause of this error message is attempting to use multiple inheritance. A class in C# may only inherit directly from one class. However, a class can implement any number of interfaces.

Example

The following example shows one way in which CS1721 is generated:

// CS1721.cs
public class A {}
public class B {}
public class MyClass : A, B {}   // CS1721

To correct this error

The following are different ways to correct this error:

  • Make class B inherit from A, and MyClass inherit from B:

    public class A {}
    public class B : A {}
    public class MyClass : B {}
    
  • Declare B as an interface. Make MyClass inherit from the interface B, and the class A:

    public class A {}
    public interface B {}
    public class MyClass : A, B {}
    

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...