473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Interface definition for static methods

Anyone able to explain to me why you cannot define an interface that can
then be implemented using static methods?

I understand the C# CLS states this, but just interested in the reasons
behind it.

thanks,
Steven
Nov 15 '05 #1
8 52730
You mean *implement* the methods of an interface with a static modifier?
Obviously the signature has to match. Or perhaps you mean having statics as
part of the interface definition?

Either way, interfaces are about defining a contract of method names that an
*instance* has to support, statics aren't instance level methods they're
class level.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Anyone able to explain to me why you cannot define an interface that can
then be implemented using static methods?

I understand the C# CLS states this, but just interested in the reasons
behind it.

thanks,
Steven

Nov 15 '05 #2
Right. Static methods can't be part of the interface definition though. Any
reason?

If i write a data layer (as i am doing...) it would be nice to define an
interface that my class can then implement using static methods, rather than
instance methods.

"John Wood" <j@ro.com> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
You mean *implement* the methods of an interface with a static modifier?
Obviously the signature has to match. Or perhaps you mean having statics as part of the interface definition?

Either way, interfaces are about defining a contract of method names that an *instance* has to support, statics aren't instance level methods they're
class level.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Anyone able to explain to me why you cannot define an interface that can
then be implemented using static methods?

I understand the C# CLS states this, but just interested in the reasons
behind it.

thanks,
Steven


Nov 15 '05 #3
So even if you could put static members in an interface and implement, how
would that be useful?

Are you going to be passing around the Type of the class, and somehow you'd
expect to be able to cast that type to an interface?

Seems a bit odd.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Right. Static methods can't be part of the interface definition though. Any reason?

If i write a data layer (as i am doing...) it would be nice to define an
interface that my class can then implement using static methods, rather than instance methods.

"John Wood" <j@ro.com> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
You mean *implement* the methods of an interface with a static modifier?
Obviously the signature has to match. Or perhaps you mean having statics as
part of the interface definition?

Either way, interfaces are about defining a contract of method names

that an
*instance* has to support, statics aren't instance level methods they're
class level.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in

message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods?

I understand the C# CLS states this, but just interested in the reasons behind it.

thanks,
Steven



Nov 15 '05 #4
Well, for a start it would make sure that the variosu data providers i have
to write all conform to a specific interface. Is that odd? I would have
though that would be pretty normal. The Data Application Blocks from MS
implement their entire data layer using static methods. I'm sure if i wanted
to write for a provider other than SqlServer I would like to just tell the
implementer to implement according to *this* interface. Would help a lot at
design time.

As for the runtime behaviour, don't expect to be able to cast anything, more
that i could do something like IData.MyStaticMethod(). I don't know that
this is so odd either. Many data layer class are implemented statically and
it would be nice to able to have some reliable interface to make sure the
static method i want to call exists!

"John Wood" <jwood8@go_ahead_remove_this.optonline.net> wrote in message
news:bs*********************@news4.srv.hcvlny.cv.n et...
So even if you could put static members in an interface and implement, how
would that be useful?

Are you going to be passing around the Type of the class, and somehow you'd expect to be able to cast that type to an interface?

Seems a bit odd.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in message news:u6**************@TK2MSFTNGP12.phx.gbl...
Right. Static methods can't be part of the interface definition though.

Any
reason?

If i write a data layer (as i am doing...) it would be nice to define an
interface that my class can then implement using static methods, rather

than
instance methods.

"John Wood" <j@ro.com> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
You mean *implement* the methods of an interface with a static modifier? Obviously the signature has to match. Or perhaps you mean having statics
as
part of the interface definition?

Either way, interfaces are about defining a contract of method names that
an
*instance* has to support, statics aren't instance level methods

they're class level.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in

message
news:%2****************@TK2MSFTNGP09.phx.gbl...
> Anyone able to explain to me why you cannot define an interface that

can > then be implemented using static methods?
>
> I understand the C# CLS states this, but just interested in the reasons > behind it.
>
> thanks,
> Steven
>
>



Nov 15 '05 #5
> As for the runtime behaviour, don't expect to be able to cast anything,
more
that i could do something like IData.MyStaticMethod(). I don't know that
But when you do IData.MyStaticMethod(), which class would it be referring
to??

I can understand that you may want to provide a good way to ensure that a
class provides a specific set of static members, but other than that I can't
see the use. You could possibly implement that type of validation using a
custom attribute.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... Well, for a start it would make sure that the variosu data providers i have to write all conform to a specific interface. Is that odd? I would have
though that would be pretty normal. The Data Application Blocks from MS
implement their entire data layer using static methods. I'm sure if i wanted to write for a provider other than SqlServer I would like to just tell the
implementer to implement according to *this* interface. Would help a lot at design time.

As for the runtime behaviour, don't expect to be able to cast anything, more that i could do something like IData.MyStaticMethod(). I don't know that
this is so odd either. Many data layer class are implemented statically and it would be nice to able to have some reliable interface to make sure the
static method i want to call exists!

"John Wood" <jwood8@go_ahead_remove_this.optonline.net> wrote in message
news:bs*********************@news4.srv.hcvlny.cv.n et...
So even if you could put static members in an interface and implement, how
would that be useful?

Are you going to be passing around the Type of the class, and somehow

you'd
expect to be able to cast that type to an interface?

Seems a bit odd.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in

message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Right. Static methods can't be part of the interface definition though.
Any
reason?

If i write a data layer (as i am doing...) it would be nice to define
an interface that my class can then implement using static methods,

rather than
instance methods.

"John Wood" <j@ro.com> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
> You mean *implement* the methods of an interface with a static modifier? > Obviously the signature has to match. Or perhaps you mean having statics as
> part of the interface definition?
>
> Either way, interfaces are about defining a contract of method names

that
an
> *instance* has to support, statics aren't instance level methods they're > class level.
>
> "Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in
message
> news:%2****************@TK2MSFTNGP09.phx.gbl...
> > Anyone able to explain to me why you cannot define an interface

that can
> > then be implemented using static methods?
> >
> > I understand the C# CLS states this, but just interested in the

reasons
> > behind it.
> >
> > thanks,
> > Steven
> >
> >
>
>



Nov 15 '05 #6
> But when you do IData.MyStaticMethod(), which class would it be referring
to??
Hey, it's a Sunday, i'm not on 100% today :) Imagining it were possible....
I may use a helper class to return a pointer to the class containing the
static/shared methods (much as i do with instance methods for just now),
except i cache instance classes for the current user (which i wouldn't for
statics obviously). So then you have your class which implements your
interface and you can call the static methods on it as always.

There is some interesting discussion on these subjects here
http://www.mail-archive.com/do******.../msg00261.html
"John Wood" <jwood8@go_ahead_remove_this.optonline.net> wrote in message
news:hQ*********************@news4.srv.hcvlny.cv.n et...
As for the runtime behaviour, don't expect to be able to cast anything,

more
that i could do something like IData.MyStaticMethod(). I don't know that


But when you do IData.MyStaticMethod(), which class would it be referring
to??

I can understand that you may want to provide a good way to ensure that a
class provides a specific set of static members, but other than that I

can't see the use. You could possibly implement that type of validation using a
custom attribute.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Well, for a start it would make sure that the variosu data providers i have
to write all conform to a specific interface. Is that odd? I would have
though that would be pretty normal. The Data Application Blocks from MS
implement their entire data layer using static methods. I'm sure if i

wanted
to write for a provider other than SqlServer I would like to just tell the
implementer to implement according to *this* interface. Would help a lot

at
design time.

As for the runtime behaviour, don't expect to be able to cast anything,

more
that i could do something like IData.MyStaticMethod(). I don't know that
this is so odd either. Many data layer class are implemented statically

and
it would be nice to able to have some reliable interface to make sure the static method i want to call exists!

"John Wood" <jwood8@go_ahead_remove_this.optonline.net> wrote in message
news:bs*********************@news4.srv.hcvlny.cv.n et...
So even if you could put static members in an interface and implement,

how would that be useful?

Are you going to be passing around the Type of the class, and somehow

you'd
expect to be able to cast that type to an interface?

Seems a bit odd.

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in

message
news:u6**************@TK2MSFTNGP12.phx.gbl...
> Right. Static methods can't be part of the interface definition though. Any
> reason?
>
> If i write a data layer (as i am doing...) it would be nice to define an
> interface that my class can then implement using static methods, rather than
> instance methods.
>
> "John Wood" <j@ro.com> wrote in message
> news:uU**************@tk2msftngp13.phx.gbl...
> > You mean *implement* the methods of an interface with a static

modifier?
> > Obviously the signature has to match. Or perhaps you mean having

statics
> as
> > part of the interface definition?
> >
> > Either way, interfaces are about defining a contract of method
names that
> an
> > *instance* has to support, statics aren't instance level methods

they're
> > class level.
> >
> > "Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in > message
> > news:%2****************@TK2MSFTNGP09.phx.gbl...
> > > Anyone able to explain to me why you cannot define an interface

that can
> > > then be implemented using static methods?
> > >
> > > I understand the C# CLS states this, but just interested in the
reasons
> > > behind it.
> > >
> > > thanks,
> > > Steven
> > >
> > >
> >
> >
>
>



Nov 15 '05 #7
Yeah that's an interesting idea (extending it to constructors). You could
extend the interface syntax to support constructors quite easily, eg.

interface IPlugin
{
this();
void RunPlug();
...
}

Personally I'd see that as more useful and easier for the C# compiler team
to implement than interfaces supporting static members.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
John Wood <jwood8@go_ahead_remove_this.optonline.net> wrote:
I can understand that you may want to provide a good way to ensure that a class provides a specific set of static members, but other than that I can't see the use. You could possibly implement that type of validation using a custom attribute.


That is indeed the only appropriate reason, but it *can* be a good
reason - including for constructors. For instance, it can be very
useful to require that a plugin class must have a parameterless
constructor so that the plugin loader can instantiate it.

I don't see how custom attributes really help in this case, to be
honest - they certainly don't help to catch things at compile time.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #8
John Wood <jw****@spaam.optonline.net> wrote:
Yeah that's an interesting idea (extending it to constructors). You could
extend the interface syntax to support constructors quite easily, eg.

interface IPlugin
{
this();
void RunPlug();
...
}

Personally I'd see that as more useful and easier for the C# compiler team
to implement than interfaces supporting static members.


It's much the same thing, I'd have thought.

The slightly strange thing is that as far as the CLR is concerned, an
interface *can* have static members.

I think it would be a bad idea to change the syntax to support one of
the two, but not the other - the reasons for including them are
basically the same.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #9

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

Similar topics

1
by: Icosahedron | last post by:
I know that virtual static methods have been addressed, and I'm not really looking for virtual methods, because I don't really need the run time dispatching. What I would like is to have compile...
7
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.
1
by: phoenix | last post by:
Hello, I guess I'm missing to logic behind the limitation or I'm doing something wrong (most likely). I have the following interface and class public interface IRBSPParser { void Parser(byte...
17
by: Tom | last post by:
This is not intuitivelly clear.
10
by: McFly Racing | last post by:
Thread Locking In Static Methods I have the need for a Log Manger class that has static methods. Normally I would use the lock statement or a Monitor statement both of which take a...
17
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...
7
by: Charles Law | last post by:
Can anyone convert the following C++ interface definition to VB.NET? I have had a go, but I cannot make the Load function work and the IsDirty function gives an error (Object not set to an...
5
by: dans | last post by:
Hi, I have recently started writing in C# on Linux using mono and come from a C++ background. I would like to create interfaces that expose methods for objects constructed using the singleton and...
1
by: kownacek | last post by:
Hello. I have some classes which resemble tables in a database. I'd like to create CRUD-like methods for each class, for example Create(), GetAll(), GetById(Guid id), DeleteById(Guid id), etc......
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.