By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,677 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,677 IT Pros & Developers. It's quick & easy.

Defining Static Members in an Interface

P: n/a
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?

Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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; };
}
}

Nov 17 '05 #2

P: n/a
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.) :-/

Nov 17 '05 #3

P: n/a
Bruce Wood <br*******@canada.com> wrote:
(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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
Ewwww... yuck. That makes my head hurt. :)

Nov 17 '05 #5

P: n/a
Bruce Wood wrote:
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

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.