问题
Is there a limitation on number of properties, methods a C# class can have?
I do a quick skim at Standard ECMA-334 and did not find any information on it.
Before jumping into why a class with many methods are bad design, I want to be more clear on the intention. Of course I will not be writing a class with large number of methods manually. The reason I am asking this is I need to generate a large number of execution units by code. I am debate between have multiple classes with single method or one large class with multiple methods.
So for this question, I am only interest if is there a limit and what is the limit for number of properties, methods.
回答1:
16.7 million per assembly per method (not class).
回答2:
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
回答3:
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!
回答4:
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.
回答5:
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
回答6:
More than you will ever want to put in a single class.
回答7:
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)
回答8:
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.
回答9:
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".
来源:https://stackoverflow.com/questions/1415665/how-many-methods-can-a-c-sharp-class-have