Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 15th, 2005, 09:42 PM
TruongLapVi
Guest
 
Posts: n/a
Default 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


  #2  
Old November 15th, 2005, 09:42 PM
Frank Oquendo
Guest
 
Posts: n/a
Default Re: Interface static member ?

TruongLapVi wrote:[color=blue]
> Hi,
>
> Why C# does not support Interface static member ?[/color]

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)


  #3  
Old November 15th, 2005, 09:42 PM
TruongLapVi
Guest
 
Posts: n/a
Default Re: Interface static member ?

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

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.


  #4  
Old November 15th, 2005, 09:43 PM
Vadym Stetsyak
Guest
 
Posts: n/a
Default Re: Interface static member ?

"TruongLapVi" <truonglapvi@hotmail.com> wrote in message
news:e3#pluf8DHA.2576@TK2MSFTNGP11.phx.gbl...[color=blue][color=green]
> > A static membre belongs to a particular class. An interface is not a[/color]
> class.[color=green]
> > Even so, interfaces are not allowed to have method definitions. That's[/color][/color]
an[color=blue][color=green]
> > implementation detail.[/color][/color]

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.
[color=blue]
> 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[/color]
this[color=blue]
> feature.[/color]

it supports this feature but not with interfaces.
Interfaces may only contain signatures for methods, properties, indexers and
events


  #5  
Old November 15th, 2005, 09:43 PM
Dmitriy Lapshin [C# / .NET MVP]
Guest
 
Posts: n/a
Default Re: Interface static member ?

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" <truonglapvi@hotmail.com> wrote in message
news:e3%23pluf8DHA.2576@TK2MSFTNGP11.phx.gbl...[color=blue][color=green]
> > A static membre belongs to a particular class. An interface is not a[/color]
> class.[color=green]
> > Even so, interfaces are not allowed to have method definitions. That's[/color][/color]
an[color=blue][color=green]
> > implementation detail.[/color]
>
> 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[/color]
this[color=blue]
> feature.
>
> Thank for your answer.
>
>[/color]

  #6  
Old November 15th, 2005, 09:44 PM
dilipdotnet at apdiya.com
Guest
 
Posts: n/a
Default Re: Interface static member ?

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:
[color=blue]
> 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 {
> ...........
> }
>[/color]

--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.