How many interfaces are allowed to be implemented?

Liviu Mandras picture Liviu Mandras · Nov 26, 2010 · Viewed 21.3k times · Source

In C#:

How many interfaces a class can implement at the same time?

public class MyClass: IInteferface_1, IInterface_2, ... , IInterface_N
{
}

Is there a limit for N?

Don't worry I don't want to implement or maintain such an object. I was just wondering if there is a limit.

Answer

Eric Lippert picture Eric Lippert · Nov 26, 2010

The C# language imposes no limit on the number of interfaces. There are two practical limits though.

First, as chibacity points out, the compiler will eventually run out of heap or stack space when processing large numbers of interfaces, or extremely deep hierarchies of interfaces.

Even if we fixed those problems, there would still be a second issue. Interface implementation is encoded in metadata in the InterfaceImpl table. Metadata tables typically can have no more than 2^24 members, so the total number of interfaces implemented by all types in an assembly must be less than about 16 million.

Obviously you are going to never run into these limitations in practice. Don't worry about it.