472,789 Members | 1,260 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 24112
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
4
by: Vai2000 | last post by:
Hi All I have a class with static data members in it. How do I un-initialize them ? Looks like if static member is called repeatedly it has residual memory from the last call. the member here is an...
5
by: rettigcd | last post by:
I have several classes that all have the same static member: class A{ public static string Table = "TableA"; } class B{ public static string Table = "TableB"; }
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
8
by: mike | last post by:
Ok, If I have the following code: //main.cpp class Base { public: int static a;
0
by: Axter | last post by:
I'm currently working on the following policy base smart pointer: http://code.axter.com/smart_ptr.h Before working on the above code, I read the following links:...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
2
dmjpro
by: dmjpro | last post by:
i have a little bit confusion on interface and static members variables in it. having static member variables in interface and non-static member variables...........what is the difference? plz...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.