允许实现多少个接口?

时间:2022-09-25 12:15:36

In C#:

在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?

N有限制吗?

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

不要担心我不想实现或维护这样的对象。我只是想知道是否有限制。

6 个解决方案

#1


39  

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

C#语言对接口数量没有限制。但有两个实际限制。

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.

首先,正如chibacity指出的那样,在处理大量接口或极深的接口层次结构时,编译器最终会耗尽堆空间或堆栈空间。

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.

即使我们解决了这些问题,仍然会有第二个问题。接口实现在InterfaceImpl表的元数据中编码。元数据表通常不能超过2 ^ 24个成员,因此程序集中所有类型实现的接口总数必须少于约1600万。

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

显然,你在实践中永远不会遇到这些限制。别担心。

#2


15  

The number of interfaces you can implement is limited by what the compiler can handle. Too many interfaces results in a * 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.

您可以实现的接口数量受编译器可以处理的内容的限制。接口太多会导致C#编译器发生堆栈溢出异常(错误CS1647)。这将使我相信没有固定限制,但在某些情况下,编译器将简单地炸弹,即编号将取决于编译器处理类时可用的堆栈空间。

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());

#3


8  

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.

据我所知,除了计算机的内存之外没有任何限制。

#4


1  

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.

考虑它对编译器/运行时实际意味着什么,说MyClass:IInteferface_1,IInterface_2,...,IInterface_N。没有设计时间限制,因为编译器只是确保您的类具有应该实现的每个接口的适当(方法)签名。至于运行时限制,我不认为内存会产生太大的影响,因为通过它实现的接口(在设计时验证)对类的任何引用都只会确保您的类具有该接口的适当方法签名。如果对象没有实现接口,则该对象将缺少方法签名。

#5


1  

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.

我刚检查了当前版本的MicrosoftC♯语言规范版本4.0,并且没有提及§1.6.4,§1.9,§10.1.4,§10.1.4.2和§13中的限制。 §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.

我显然没有阅读整个500页,但我不知道该文件中的其他地方可能会提到限制。

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.

注意:这仅适用于MicrosoftC♯,仅适用于4.0版。我没有检查MicrosoftC♯的早期版本,也没有检查ECMA / ISOC♯。此外,它仅适用于C♯,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).

最后但并非最不重要的是,Microsoft VisualC♯和NovellMonoC♯中可能存在特定于实现的限制,以及Microsoft和Mono的CLI实现(即CLR和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.

然而,问题是关于C♯,不是关于C♯的任何特定实现而不是关于CLI,所以我非常有信心声称一个类可以实现的接口数量是无限的。

#6


-5  

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.

您被限制为255,因为单个字节被JIT用作相关接口的类型表的索引器。

#1


39  

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

C#语言对接口数量没有限制。但有两个实际限制。

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.

首先,正如chibacity指出的那样,在处理大量接口或极深的接口层次结构时,编译器最终会耗尽堆空间或堆栈空间。

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.

即使我们解决了这些问题,仍然会有第二个问题。接口实现在InterfaceImpl表的元数据中编码。元数据表通常不能超过2 ^ 24个成员,因此程序集中所有类型实现的接口总数必须少于约1600万。

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

显然,你在实践中永远不会遇到这些限制。别担心。

#2


15  

The number of interfaces you can implement is limited by what the compiler can handle. Too many interfaces results in a * 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.

您可以实现的接口数量受编译器可以处理的内容的限制。接口太多会导致C#编译器发生堆栈溢出异常(错误CS1647)。这将使我相信没有固定限制,但在某些情况下,编译器将简单地炸弹,即编号将取决于编译器处理类时可用的堆栈空间。

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());

#3


8  

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.

据我所知,除了计算机的内存之外没有任何限制。

#4


1  

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.

考虑它对编译器/运行时实际意味着什么,说MyClass:IInteferface_1,IInterface_2,...,IInterface_N。没有设计时间限制,因为编译器只是确保您的类具有应该实现的每个接口的适当(方法)签名。至于运行时限制,我不认为内存会产生太大的影响,因为通过它实现的接口(在设计时验证)对类的任何引用都只会确保您的类具有该接口的适当方法签名。如果对象没有实现接口,则该对象将缺少方法签名。

#5


1  

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.

我刚检查了当前版本的MicrosoftC♯语言规范版本4.0,并且没有提及§1.6.4,§1.9,§10.1.4,§10.1.4.2和§13中的限制。 §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.

我显然没有阅读整个500页,但我不知道该文件中的其他地方可能会提到限制。

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.

注意:这仅适用于MicrosoftC♯,仅适用于4.0版。我没有检查MicrosoftC♯的早期版本,也没有检查ECMA / ISOC♯。此外,它仅适用于C♯,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).

最后但并非最不重要的是,Microsoft VisualC♯和NovellMonoC♯中可能存在特定于实现的限制,以及Microsoft和Mono的CLI实现(即CLR和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.

然而,问题是关于C♯,不是关于C♯的任何特定实现而不是关于CLI,所以我非常有信心声称一个类可以实现的接口数量是无限的。

#6


-5  

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.

您被限制为255,因为单个字节被JIT用作相关接口的类型表的索引器。