Statics & Interfaces

  • Thread starter Thread starter hufaunder
  • Start date Start date
H

hufaunder

I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?

Thanks
 
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?

The CLR allows static methods to be implemented in interfaces, but even
then you couldn't just specify a static method which has to be
implemented by the type implementing the interface. C# doesn't even let
you put the static method in there in the first place.

I agree it would be useful sometimes, but consider: how would you call
the static method in the first place?
 
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?

Maybe you should make an abstract class instead?
 
Hi

First of all we should understand the keyword static; static says that
it would be having its own memory area irrespective of its instance
means this function will be loaded into the memory without having its
instance.

Now think if we have an interface and have one static function declared
in it without body. Now what will be loaded into the memory and if user
will call IInterface.StaticFunction(), what will be executed. So I
think its totally useless having a static function in interface.


-Rohit
 
An interface defines just a contract and not the implementation itself.
So if the static keyword would be allowed for a function then it would
only say that the class implementing the interface must declare this
function as static. Therefore, unless I missed something I think your
argument is not valid.
 
Good point. Don't know why I didn't think of this myself.. Thanks for
the tip. I think that might work.
 
An interface defines just a contract and not the implementation itself.
So if the static keyword would be allowed for a function then it would
only say that the class implementing the interface must declare this
function as static. Therefore, unless I missed something I think your
argument is not valid.

The "interface as a contract" is a useful concept, but not 100% correct.

Really, an interface is a restricted abstract class, with all functions
being pure virtual and no data. So the interface has a v-table layout which
needs to be followed by implementing classes, just like an abstract class
has a v-table layout used by subclasses.

Now, just like the Region class has a static member function Union which
operates on a group of Region objects, MSIL allows an interface to have
static member functions and properties which are generally useful and
applied to the entire tree of class types implementing that interface.

For example, if an interface declared an instance Guid property, it would be
useful to have a static method which returns the instance (of some derived
type) given the Guid. This static method is part of the interface type, and
not a contractual constraint on implementors.

The static keyword consistently means "part of the declaring type, not part
of any object". Thus you can see that an abstract static method is
impossible.

To get something like static methods on derived types, you can borrow from
COM's playbook. In COM implementations in C (not C++), you had to set up
the object's v-table by declaring a structure of function pointers. You can
do the same, by having a class of delegate fields. The interface declares a
property with that class type. Each implementing class needs to initialize
one copy of this class in the type initializer, to point to its set of
static methods, and then the (instance) property just returns the static
field which refers to the delegate group.

Something like:

class RequiredStaticMethods
{
public readonly EventHandler a;
public readonly EventHandler b;
public RequiredStaticMethods(EventHandler a, EventHandler b)
{
if (a == null || b == null) throw new ArgumentNullException();
this.a = a;
this.b = b;
}
}

interface INeedStaticMethods
{
RequiredStaticMethods StaticMethods { get; }
}

class AnImplementation : INeedStaticMethods
{
private static void OnA(object sender, EventArgs e) {}
private static void OnB(object sender, EventArgs e) {}
public static readonly RequiredStaticMethods StaticMethods = new
RequiredStaticMethods(OnA, OnB);
RequiredStaticMethods INeedStaticMethods.StaticMethods { get { return
StaticMethods; } }
}

now you can call:
AnImplementation.StaticMethods.a(...);
INeedStaticMethods x = new AnImplementation(); // call static method of
fixed type
x.StaticMethods.b(...); // dynamic dispatch to static method based on
runtime type of x
 
An interface defines just a contract and not the implementation itself.
So if the static keyword would be allowed for a function then it would
only say that the class implementing the interface must declare this
function as static. Therefore, unless I missed something I think your
argument is not valid.

Except that in the CLR, an interface *can* have a static method - which
must have an implementation. (i.e. it's not the same type of beast as
the rest of an interface).
 
Thanks for the detailed explanation
Ben said:
The "interface as a contract" is a useful concept, but not 100% correct.

Really, an interface is a restricted abstract class, with all functions
being pure virtual and no data. So the interface has a v-table layout which
needs to be followed by implementing classes, just like an abstract class
has a v-table layout used by subclasses.

Now, just like the Region class has a static member function Union which
operates on a group of Region objects, MSIL allows an interface to have
static member functions and properties which are generally useful and
applied to the entire tree of class types implementing that interface.

For example, if an interface declared an instance Guid property, it would be
useful to have a static method which returns the instance (of some derived
type) given the Guid. This static method is part of the interface type, and
not a contractual constraint on implementors.

The static keyword consistently means "part of the declaring type, not part
of any object". Thus you can see that an abstract static method is
impossible.

To get something like static methods on derived types, you can borrow from
COM's playbook. In COM implementations in C (not C++), you had to set up
the object's v-table by declaring a structure of function pointers. You can
do the same, by having a class of delegate fields. The interface declares a
property with that class type. Each implementing class needs to initialize
one copy of this class in the type initializer, to point to its set of
static methods, and then the (instance) property just returns the static
field which refers to the delegate group.

Something like:

class RequiredStaticMethods
{
public readonly EventHandler a;
public readonly EventHandler b;
public RequiredStaticMethods(EventHandler a, EventHandler b)
{
if (a == null || b == null) throw new ArgumentNullException();
this.a = a;
this.b = b;
}
}

interface INeedStaticMethods
{
RequiredStaticMethods StaticMethods { get; }
}

class AnImplementation : INeedStaticMethods
{
private static void OnA(object sender, EventArgs e) {}
private static void OnB(object sender, EventArgs e) {}
public static readonly RequiredStaticMethods StaticMethods = new
RequiredStaticMethods(OnA, OnB);
RequiredStaticMethods INeedStaticMethods.StaticMethods { get { return
StaticMethods; } }
}

now you can call:
AnImplementation.StaticMethods.a(...);
INeedStaticMethods x = new AnImplementation(); // call static method of
fixed type
x.StaticMethods.b(...); // dynamic dispatch to static method based on
runtime type of x
 
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?

I just whining abou this the other day in a blog entry. It's a feature I
would quite like to see, as well as some other realated stuff.

http://www.coversant.net/dotnetnuke/Default.aspx?tabid=88&EntryID=14

Unfortunatly, all I have is questions and suggestions - no good solutions.
 
Chris,

Actually, after the previous posts I thought abstract classes would be
the solution. It turns out they are not, at least not for my
intentions. What I would have liked is an abstract static method, just
as you suggested in your blog. Unfortunately, that is not possible.
 
Back
Top