开发者

How many interfaces are allowed to be implemented?

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.


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.


The number of interfaces you can implement is limited by what the compiler can handle. Too many interfaces results in a stackoverflow exception in the C# compiler (error CS1647). This would lead me to believe that there is no fixed limit, but that under certain conditions the compiler will simply bomb i.e. the number will be dependant on what stack space is available when the compiler is handling the class.

The limit is likely to be compiler version dependant too. The following code can be used to generate a test case for probing the limit.

    int iterations = (int)Math.Pow(2, 8) + 1;

    Func<int, string> getInterfaceName = i => "I" + i;

    StringBuilder sb = new StringBuilder();

    sb.AppendLine("using NUnit.Framework;");
    sb.AppendLine("[TestFixture]");

    sb.AppendLine("public class Test");
    sb.AppendLine("{");

    sb.AppendLine("[Test]");
    sb.AppendLine("public void bling()");
    sb.AppendLine("{");
    sb.AppendLine("Class1 class1 = new Class1();");

    for (int i = 0; i < iterations; i++)
    {
        sb.AppendLine(getInterfaceName(i) + " int" + i + " = class1;");
        sb.AppendLine("int" + i + ".Bling();");
    }

    sb.AppendLine("}");

    for (int i = 0; i < iterations; i++)
    {
        sb.AppendLine("public interface " + getInterfaceName(i) + " { void Bling(); }");
    }

    sb.Append("public class Class1 : " + getInterfaceName(0));

    for (int i = 1; i < iterations; i++)
    {
        sb.Append(", " + getInterfaceName(i));
    }

    sb.Append("{ public void Bling(){} }");

    sb.AppendLine("}");

    File.WriteAllText(@"C:\tmp.cs", sb.ToString());


If you're asking this question with a view to actually implementing a LOT of interfaces, I'd say you have a serious design problem.

As far as I know, there is no limit other than your computer's memory.


Consider what it actually means to the compiler / run time to say MyClass: IInteferface_1, IInterface_2, ... , IInterface_N. There is no design time limit as the compiler simply makes sure that your class has appropriate (method) signatures for each interface its supposed to implement. As for a run time limit, I don't think memory has much impact as any reference to your class via an interface it implements (as verified at design time) would simply make sure your class has the appropriate method signature for that interface. If the object doesn't implement the interface, the object would just lack the method signature.


I just checked the current version of the Microsoft C♯ Language Specification Version 4.0, and there is no mention of a limit in §1.6.4, §1.9, §10.1.4, §10.1.4.2 and §13. There is also no syntactic limit in the grammar in §B.2.7.

I obviously didn't read the entire 500 pages, but I don't know where else in that document a limit might be mentioned.

Note: this only applies to Microsoft C♯, and only to Version 4.0. I didn't check earlier versions of Microsoft C♯, nor did I check ECMA/ISO C♯. Also, it only applies to C♯, there might be limits in the CLI.

And last but not least, there might be implementation-specific limits in both Microsoft Visual C♯ and Novell Mono C♯, as well implementation-specific limits in Microsoft's and Mono's implementations of the CLI (i.e. the CLR and the Mono VM).

However, the question was about C♯, not about any particular implementation of C♯ and not about the CLI, so I feel pretty confident in claiming that the number of interfaces a class can implement is unbounded.


There is a limit

You are limited to 255 as a single byte is used as an indexer by the JIT to the type table for the relevant interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜