[Mono-dev] Constructor implementation obligation via interface?

Jb Evain mono at evain.net
Wed May 24 08:18:17 EDT 2006


Kamil Skalski wrote:
> There is a slight problem. In C# empty constructors are added
> automatically, so you can't define a class without empty constructor.

The default constructor is added only if there is not valued constructor 
defined. So it's possible to define a class without the default constructor.

> 2006/5/24, Ympostor <ympostor at clix.pt>:
>> I have a question about C# 2.0:
>> If I want the compiler to show an error if a class A does not implement
>> the function void B(), I can make an interface that contains this method
>> and make class A inherit from that interface.
>> But, how can I do it if I want the compiler to show an error if class A
>> doesn't have an empty constructor. Can this be controlled statically?

That's tricky/ugly but you can use generics to check this:

interface IMustHaveDefaultParameter<T> where T : new () {}

class MyClass : IMustHaveDefaultParameter<MyClass> {

	public MyClass ()


More information about the Mono-devel-list mailing list