Reputation: 91885
I'm writing a really simple IoC/DI container, and I've got the following code:
ConstructorInfo[] ctors = concreteType.GetConstructors();
if (ctors.Length == 0)
return Activator.CreateInstance(concreteType);
// more code goes here...
I can't come up with a test case that results in a type having zero constructors, even with this:
class LonelyType {}
Is it possible for a .NET type to have no constructors, or does the CLR always provide an implicit one?
Upvotes: 2
Views: 126
Reputation: 1502216
Yes - static classes have no constructors:
static class VeryLonelyType{}
The normal parameterless constructor is provided by the C# compiler, not the CLR.
Note that as far as the CLR is concerned, structs generated from C# don't have parameterless constructors either. For example:
struct Foo{}
won't contain a parameterless constructor. You can do it in IL, which can prove interesting...
Upvotes: 8