How many methods can a C# class have

拥有回忆 提交于 2019-11-29 09:16:10

16.7 million per assembly per method (not class).

I don't know how many methods a C# class can have, but I do know that when you're thinking about it you are most certainly doing something wrong.

If there is a limit(which I doubt) it's so high that you won't exceed it. Except you have a really bad class design.

See the anti-pattern "God object".

UPDATE:

Even though I still don't exactly know what you want to achieve, I still believe that you should definitely create a lot of classes with only a few methods for the following reasons:

  • Performance: if you are putting all properties into one class, for every property memory has to be allocated when you create an instance, even if you only need 5% of the properties in your class

  • Modularity: if you create a lot of classes you can make them all implement an interface/abstract class and thereby define a similar structure, which will help making your application more modular

  • Structure: it's pretty straightforward to see which methods use which properties when only they reside in the same class - otherwise things might get really really messy

  • Compiling time: when changing the implementation of one function, the others don't have to be re-compiled, as they are in other classes

The correct answer, in the current CLR implementation, is ushort.MaxValue - 15. This can be tested as follows:

AppDomain appDomain = AppDomain.CurrentDomain;

AssemblyName aname = new AssemblyName ("MyDynamicAssembly");

AssemblyBuilder assemBuilder =
  appDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Run);

ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule ("DynModule");

TypeBuilder tb = modBuilder.DefineType ("Widget", TypeAttributes.Public);

for (int i = 0; i < ushort.MaxValue - 15; i++)
{
    MethodBuilder methBuilder = tb.DefineMethod ("SayHello" + i, MethodAttributes.Public, null, null);
    ILGenerator gen = methBuilder.GetILGenerator();
    gen.EmitWriteLine ("Hello world");
    gen.Emit (OpCodes.Ret);
}
Type t = tb.CreateType();
object o = Activator.CreateInstance (t);

The question is relevant if you're using Reflection.Emit to create a typed DataContext to back a database (as LINQPad does). With enough stored procedures, you can hit this limit!

The Type.GetMethods method returns an array that must be indexed by an integer so, I would say you can't have more than int.MaxValue methods per class.

Dmitry

Look at this: https://softwareengineering.stackexchange.com/questions/193295/implications-of-a-large-partial-class/193304?noredirect=1#193304

Someone actually went through the pain of generating a class with as many methods as possible

More than you will ever want to put in a single class.

Yes...

Its called common sense. Try not to overload a class, it will most likely violate the single responsibility principle, and noone will be able to understand it.

After all, a class is there "only to aid the developer, who cant fit more then 7 informations at once into his short term memory" (Yes, i know thats a dangerous statement)

I don't think so. However, good software development practices and guidelines when followed and considered should naturally limit the number of properties and methods that a class has to what makes sense and absolute necessity. Such practices include SOLID, KISS (keep it simple), DRY (Don't repeat yourself), composition, refactoring, inheritance, etc.

I have an automated code generation tool and I currently hit the limit between ~5K (which worked) and 10K, which failed

System.TypeLoadException: Type '<the type name>' from assembly '<the assembly name>, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' contains more methods than the current implementation allows.

So this contradicts claims that it is "unlimited".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!