|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Statics & InterfacesI 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 <hufaun***@yahoo.com> wrote:
> I have an interface where I would like to have some static functions. The CLR allows static methods to be implemented in interfaces, but even > 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? 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? -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too 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 Jon wrote: Show quote > <hufaun***@yahoo.com> wrote: > > 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? > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too 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. Rohit wrote: Show quote > 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 > > Jon wrote: > > <hufaun***@yahoo.com> wrote: > > > 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? > > > > -- > > Jon Skeet - <sk***@pobox.com> > > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > > If replying to the group, please do not mail me too <hufaun***@yahoo.com> wrote in message
news:1157640328.484248.192830@b28g2000cwb.googlegroups.com... The "interface as a contract" is a useful concept, but not 100% correct.> 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. 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 Thanks for the detailed explanation
Ben Voigt wrote: Show quote > news:1157640328.484248.192830@b28g2000cwb.googlegroups.com... > > 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 <hufaun***@yahoo.com> wrote:
> An interface defines just a contract and not the implementation itself. Except that in the CLR, an interface *can* have a static method - which > 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. must have an implementation. (i.e. it's not the same type of beast as the rest of an interface). -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too <hufaun***@yahoo.com> wrote in message
news:1157608659.592263.96770@b28g2000cwb.googlegroups.com... Maybe you should make an abstract class instead?>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? Show quote > > Thanks > Good point. Don't know why I didn't think of this myself.. Thanks for
the tip. I think that might work. Michael C wrote: Show quote > news:1157608659.592263.96770@b28g2000cwb.googlegroups.com... > >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? > > > > > Thanks > > <hufaun***@yahoo.com> wrote:
>I have an interface where I would like to have some static functions. I just whining abou this the other day in a blog entry. It's a feature I > 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? 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 Mullins 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. Chris Mullins wrote: Show quote > >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 Mullins |
|||||||||||||||||||||||