Is there any limit on number of classes that a namespace can have in .NET? Further what is the recommended number of classes that there should be in a namespace?
Two classes with the same name can be created inside 2 different namespaces in a single program. Inside a namespace, no two classes can have the same name.
In c#, we can define and access multiple namespaces in our application with using keyword. To access the custom namespace classes, we need to import the custom namespace with using keyword and need to create an instance for that classes in our application.
There's no specified maximum number of classes "per namespace"-- a namespace is really just a part of the Type's full name, not a logical entity in the CLR
The recommended number is whatever makes sense: use namespaces to group logically related classes together.
I'm sure that if you have enough types you can run the compiler or the runtime out of memory, but that's a physical limit not a specification - and it probably doesn't matter if they are in the same namespace or not.
Note that as Steven points out, you can also have the same namespace present in multiple assemblies as well.
I tried it out: I just built an assembly containing 1,000,000 types without any problem. However at 5,000,000 the C# compiler ran out of memory :-).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With