473,388 Members | 1,399 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,388 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 24168
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.