473,497 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Constructor in Interface

Constructor in a Interface is possible?
If yes, what's the C# syntax?
Aug 17 '06 #1
5 41327
No. Not possible. Interfaces are never created.

Mike
http://www.seeknsnatch.com

"Cristiano de Pádua Milagres Oliveira" <cr****************@poli.usp.br>
wrote in message news:OH**************@TK2MSFTNGP04.phx.gbl...
Constructor in a Interface is possible?
If yes, what's the C# syntax?

Aug 17 '06 #2
No, it isn't supported directly, as CLR interfaces operate only on
instances that already exist (hence no ctors nor statics) You could,
however, provide a factory method on the interface (or a second
interface), i.e.

public interface ISomethingFactory {
ISomething Create();
}

Note also that with generics (2.0), the presence of a default ctor can
be specified with the new() clause, e.g.

public void SomeMethod<T>(T something) where T : ISomething, new() {}

Marc

Aug 17 '06 #3
Maybe I hadn't explain correctly.
What I want know is if is possible define in the Interface the header of the
Class Constructor that is inheriting..

"Cristiano de Pádua Milagres Oliveira" <cr****************@poli.usp.br>
escreveu na mensagem news:OH**************@TK2MSFTNGP04.phx.gbl...
Constructor in a Interface is possible?
If yes, what's the C# syntax?

Aug 17 '06 #4
No, it isn't. Concrete types define their ctors along their own needs.
After all, you can only (with the exception of where T : new()) create
the actual object by using the concrete ctor (even if using a factory),
or by using reflection.

So no. Not by interfaces.

Marc

Aug 17 '06 #5

I think you're saying

"Can I .. in the Interface .. force a concrete class to implement a certain
constuctor?"

The answer is no. I've found myself at times thinking "That would be nice".

I think the small work around is

interface ICheckMeOut
abstract void Initialize(string abc);
then your concrete has to implement that.. and you call that in your
concrete constructors.

not really clean, I know.


"Cristiano de Pádua Milagres Oliveira" <cr****************@poli.usp.br>
wrote in message news:%2****************@TK2MSFTNGP04.phx.gbl...
Maybe I hadn't explain correctly.
What I want know is if is possible define in the Interface the header of
the
Class Constructor that is inheriting..

"Cristiano de Pádua Milagres Oliveira" <cr****************@poli.usp.br>
escreveu na mensagem news:OH**************@TK2MSFTNGP04.phx.gbl...
Constructor in a Interface is possible?
If yes, what's the C# syntax?


Aug 17 '06 #6

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

Similar topics

21
10102
by: Steve - DND | last post by:
I know it's possible to require inheriting classes to implement a particular function or property, but is it possible to require a inheriting class to implement a constructor of it's own? ...
10
2222
by: linkspeed | last post by:
Following texts are from C# spec. The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this...
4
17164
by: Peter | last post by:
I want to copy a parent class instance's all datas to a child's. It's actually a C++'s copy constructor. But why the following code does not work - there is a compile error! How it should look...
20
4222
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
14
2693
by: Arne | last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that be a clone method?
8
5223
by: Sam Kuehn | last post by:
How do I accomplish the fallowing (is it even possible). Say I write a UserControl "MyControl.ascx". Now I use LoadControl("MyControl.ascx"). But I really want MyControl to require parameters in...
1
6791
by: Ruffin Bailey | last post by:
I want to declare an interface that forces the presence of a certain constructor (in this case, "Sub New(ByVal ds As DataSet)"). When I try, I get a 'Sub New' cannot be declared in an...
0
978
by: Eric_Dexter | last post by:
I was looking at vpython and it would seem to be an easy thing to add to boa-constuctors classes but I am not sure if that is possible within the boa-constructor interface. Is it possible to do...
2
3877
by: gilbert | last post by:
Hello. I am trying to use c# generic to define a class. class MyClass<Twhere T: new(){ } In this definition, MyClass can create T objects with a default constructor. Is there any way to...
8
2070
by: rn5a | last post by:
Suppose I have the following class code: Imports System Imports System.Data Imports System.Data.SqlClient Public Class DBSettings Private sqlCmd As SqlCommand Private sqlConn As...
0
6991
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...
0
7196
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...
0
7373
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...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
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...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
286
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...

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.