473,769 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interfaces and Constructors

Okay, so I know this will come off as a stupid question...but I am going to
ask it anyways...

I know you are not allowed to have constructors defined in an interface, but
why not?

I really dont need ways/design patterns around this issue. What I want to
know is why the CLR does not allow this.

Regards,
Adam
Oct 18 '06 #1
4 1621
Adam,
>I know you are not allowed to have constructors defined in an interface, but
why not?
How would you use it if it was possible? Interfaces themself can't be
instantiated and they can't have any implementation anyway. Having
constructors in an interface to enforce implementing classes to
provide such constructors doesn't make much sense either, since you
have to specify a concrete class when creating an object.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 18 '06 #2
Directly from MSDN:
"Interfaces describe a group of related behaviors that can belong to any
class or struct."

An interface is just a way to describe a contract by which a class will
implement given functionality. A constructor is the primary way to manage
all your behavior using initialization.

I am not looking to instantiate an interface, I am looking to guarantee a
class will implement a specific signature for a constructor.

public interface IBusinessObject <VOwhere VO : IValueObject
{
constr(VO valueObject)
}

And an added benefit of interfaces, is that they are a great way to
establish development standards. Once again why not?

Oct 18 '06 #3
Adam,

The reason why interfaces don't define constructors is because they're
suppose to be type agnostic. But, to create and initialize an object
you *have* to know its type. However, you can abstract that away from
where it's used by taking advantage of one of the factory design
patterns. So if you want to guarentee that an IBusinessObject is
created in some special way then use a factory...say
IBusinessObject Factory which has a parameterized Create method.

Brian

Adam wrote:
Directly from MSDN:
"Interfaces describe a group of related behaviors that can belong to any
class or struct."

An interface is just a way to describe a contract by which a class will
implement given functionality. A constructor is the primary way to manage
all your behavior using initialization.

I am not looking to instantiate an interface, I am looking to guarantee a
class will implement a specific signature for a constructor.

public interface IBusinessObject <VOwhere VO : IValueObject
{
constr(VO valueObject)
}

And an added benefit of interfaces, is that they are a great way to
establish development standards. Once again why not?
Oct 19 '06 #4
"Adam" <Ad**@discussio ns.microsoft.co ma écrit dans le message de news:
A2************* *************** **...icrosof t.com...

| I know you are not allowed to have constructors defined in an interface,
but
| why not?

This is not just a restriction in the CLR, other non-.NET languages also
don't allow this. Thinking it through, a constructor is invoked on a type,
not an instance, and it is important that this distinction is made.

Not only can you not define constructors in an interface, you cannot define
static methods either. This is for the same reason that they are not members
of an implementing object, they are members of the *type* of that object.

It may be helpful to think of this as being two classes: one for the type
and another for the instance. Delphi has the concept of a class reference,
which is essentially a "static" class, but upon which, you can call, not
only constructors and static methods, but virtual constructors and static
methods.

If you realise that constructors and static methods belong on a "reference"
class, then you can emulate the Delphi behaviour of allowing virtual
constructors and static methods by creating your own reference or metaclass.
Then, you also have the possibility of implementing an interface on the
metaclass.

Rough example :

public interface IMyInterface
{
MyClass Create();
}

public class MyClass
{
public class Reference : IMyInterface
{
public MyClass Create()
{
return new MyClass();
}

private MyClass() { }
}

public static Reference Ref
{
get { return new Reference(); }
}
}

void Test()
{
IMyInterface ref = MyClass.Ref;

MyClass cls = ref.Create();

...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Oct 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
5806
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
7
17194
by: mdc | last post by:
Hi, Is there any way to implement an interface as a static method in C#? If not, is this a bug? Micheal.
19
4589
by: Duncan McNutt .[FTSE] | last post by:
Why does code derive from for example, IComparer ie, class SomeClass : IComparer { public SomeClass() { }
12
12469
by: Anders Borum | last post by:
Hello! I was wondering why we're allowed to define interfaces as internal, when all the members of the interface, when implemented in a class, are made public? I know that interfaces serve as contracts, but why wouldn't you be allowed to have internal contracts also, not necessarily exposing the members publicly? You're not allowed to cast an object to the internal interface, but I'm unable to keep the members internal.
17
2355
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
3
1638
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all permutations for constructors (none, default, two argument) and method call in body of constructor (none and one). Maybe this example is not representative, but for this example I found the following: 1. Without any constructors, the program works fine...
7
2956
by: hufaunder | last post by:
I have an interface for which I want to define a particular constructor. I want to force the user to implement this particular constructor. Even better would be if I could make sure there is no default constructor. That might be a bit messy because the implementer of the interface should have the freedom to add any function/constructor he wants on top of the interface. But can't I at least force that one particular constructor must be...
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.