Anu Viswan

hi

can i specify in a interface or something that the class that use it should implement a constructor with certain parameter

I want to say something like this, that, the classes should implement a constructor that would take a certain signature for sure.

is it possible through interface or any other way




Re: Visual C# Language enfore constructor with signature

Christian Liensberger - MSP

That's not possible via an interface. In an interface you can only define methods, properties, etc. - You can use generics to specify that a method can only take an object that has an empty constructor (but also not way to specify a list of parameters).

You could create an abstract base class that defines a certain constructor. But that still doesn't mean that the inherited classes have that constructor. They could just define a new constructor and call the base constructor that takes the arguments.

There is actually no way to force a class to have a constructor with certain arguments.






Re: Visual C# Language enfore constructor with signature

Vivek Ragunathan

Hi

There is no compile time feature for that. But if you are trying to create object instances thru reflection Activator.CreateInstance, the creation will throw exception. If such a thing is ok enough for you. Otherwise it is just a discipline to be followed by the class writer and the class user.

Regards