Alex Rivera | Logout

How many methods can a C# class have

Asked 2009-09-12T17:30:54.993
16

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.

Edit
Report

1 Answer

15

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

answered 2009-09-12T17:35:11.170

Your Answer