Defining Static Members in an Interface

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to declare a method of an interface as static?

For instance, can I create an interface that defines a static method and 2
instance methods?
 
Unfortunately, that is not possible. Static members cannot be abstract,
and that's what an interface is basically defining: a set of abstract
methods.

But you can get the same effect by using a nested interface. Something
like:

interface IStaticMethods
{
void StaticMethod1();
}

interface IMyInterface
{
IStaticMethods StaticMethods
{
get;
}
}

internal class StaticMethodsImp
: IStaticMethods
{
public override void StaticMethod1()
{
// implementation goes here.
}
}

public class MyClass
: IMyInterface
{
private StaticMethodsImp m_staticMethods
= new StaticMethodsImp();
public override IStaticMethods StaticMethods
{
get { return m_staticMethods; };
}
}
 
No. Static members do not participate in inheritance and therefore do
not participate in polymorphism. Since you must always refer to a
static member via its defining class, it's not clear what declaring a
static member in an interface would mean. It might mean:

1. That you should call the static member using the interface name, in
which case the static member should be _defined_ in the interface:
signature, code, and all.

or

2. Every class that implements the interface must define the static
member. But then you can't refer to the static member polymorphically,
so what's the point?

(Notwithstanding this I had a surreal experience recently in which
Visual Studio was allowing me to refer to static methods in a parent
class using the child class's class name. I'm still wondering what
happened there: that's not supposed to be allowed.) :-/
 
Bruce Wood said:
(Notwithstanding this I had a surreal experience recently in which
Visual Studio was allowing me to refer to static methods in a parent
class using the child class's class name. I'm still wondering what
happened there: that's not supposed to be allowed.) :-/

Yes it is - i.e. you can use UnicodeEncoding.ASCII, even though the
ASCII property is declared in Encoding.

The first part of member lookup is:

<quote>
First, the set of all accessible (§10.5) members named N declared in T
and the base types (§14.3.1) of T is constructed.
</quote>

I think it's a bad idea to refer to a static member via a derived type
name, but it's legal.
 
Bruce said:
No. Static members do not participate in inheritance and therefore do
not participate in polymorphism.

This has bothered me for a while. I wonder why Anders left this feature
out of C# - Delphi supports virtual class methods (i.e. virtual static
methods), as well as virtual constructors, which you can call through
metaclass types. You can get the same effect by using nested
interfaces, factory classes, etc., but virtual class methods are much
cleaner.

OTOH, even in Delphi, you can't put class methods in an interface.
You'd have to use a base class, something like this:

type
TBase = class
public
class procedure MyMethod; virtual; abstract;
end;

TDerived1 = class(TBase)
public
class procedure MyMethod; override;
end;
TDerived2 = class(TBase)
public
class procedure MyMethod; override;
end;

// now to use MyMethod polymorphically...
procedure CallMyMethod;
var
whichClass: class of TBase;
begin
if SomeCondition then
whichClass := TDerived1
else
whichClass := TDerived2;

whichClass.MyMethod;
end;

Jesse
 
Back
Top