views:

55

answers:

1

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?

+7  A: 

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...

Jon Skeet
Beat me to it... :-P
LBushkin
Oh reminds me of http://www.youtube.com/watch?v=xrxlnBJ4R0o
DrG