470,596 Members | 1,594 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Interface static member ?

Hi,

Why C# does not support Interface static member ?

Some time I want implement NullObject Pattern:

public interface INullObject {
public static INullObject Null {
get { return NullObject.Instance; } // !!! Wrong, C# not support ?
}
}
public class NullObject {
private static instance; // Singleton pattern
private NullObject () {};
static NullObject() { instance = new NullObject; }
public static NullObject Null {
get { instance; )
}
}
}
public class NullObjectImpl : INullObject, FooClass {
...........
}

--
Vi
Nov 15 '05 #1
5 23931
TruongLapVi wrote:
Hi,

Why C# does not support Interface static member ?


A static membre belongs to a particular class. An interface is not a class.
Even so, interfaces are not allowed to have method definitions. That's an
implementation detail.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
> A static membre belongs to a particular class. An interface is not a
class.
Even so, interfaces are not allowed to have method definitions. That's an
implementation detail.


I think that static member is not instance member, ie we needn't creat new
instance for calling static member, so I expect that C# should support this
feature.

Thank for your answer.
Nov 15 '05 #3
"TruongLapVi" <tr*********@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP11.phx.gbl...
A static membre belongs to a particular class. An interface is not a class.
Even so, interfaces are not allowed to have method definitions. That's an implementation detail.


interface is a class declared as abstract class and all the methods defined
in it are also abstract.
Interfaces define mereley methods and classes that implement interfaces must
implement those methods.
I think that static member is not instance member, ie we needn't creat new
instance for calling static member, so I expect that C# should support this feature.


it supports this feature but not with interfaces.
Interfaces may only contain signatures for methods, properties, indexers and
events
Nov 15 '05 #4
Interfaces cannot contain static members because members declared in an
interface are inheretly virtual since their polymorphic implementation is
assumed. And a static member cannot be virtual.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"TruongLapVi" <tr*********@hotmail.com> wrote in message
news:e3****************@TK2MSFTNGP11.phx.gbl...
A static membre belongs to a particular class. An interface is not a class.
Even so, interfaces are not allowed to have method definitions. That's an implementation detail.


I think that static member is not instance member, ie we needn't creat new
instance for calling static member, so I expect that C# should support

this feature.

Thank for your answer.


Nov 15 '05 #5
Hello Vi,
Interface only describes behaviour and serves the purpose of describing
a contractual obligation for classes implementing the interface that
certain behaviour is implemented.
The .net framework is not capable of downcasting objects to their
interfaces. If for example object A implements interface I1 and I2 the
framework will not be able to intelligently downcast the object to I1 or
I2. whereas if an abstract class is used there is no issue as there is
no multiple inheritence.
Hope that makes more sense than its confusing
Thanks
TruongLapVi wrote:
Hi,

Why C# does not support Interface static member ?

Some time I want implement NullObject Pattern:

public interface INullObject {
public static INullObject Null {
get { return NullObject.Instance; } // !!! Wrong, C# not support ?
}
}
public class NullObject {
private static instance; // Singleton pattern
private NullObject () {};
static NullObject() { instance = new NullObject; }
public static NullObject Null {
get { instance; )
}
}
}
public class NullObjectImpl : INullObject, FooClass {
...........
}


--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by Joost Ronkes Agerbeek | last post: by
15 posts views Thread by cppaddict | last post: by
4 posts views Thread by Vai2000 | last post: by
8 posts views Thread by mike | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.