implementing interfaces with static methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mdc

    #1

    implementing interfaces with static methods

    Hi,

    Is there any way to implement an interface as a static method in C#? If
    not, is this a bug?

    Micheal.


  • Chris Capel

    #2
    Re: implementing interfaces with static methods

    The interface itself determines whether the method should be static or
    instance. If an application had an instance of an object with interface
    IFoo, and it wanted to call IFoo.X() on the object, just imagine what would
    happen if the class implemented IFoo.X() as a static function. You can
    declare that an interface member is static in the interface definition, but
    you have implement it the way it is declared. This is correct behavior.

    Chris

    "mdc" <mdc@microsoft. com> wrote in message
    news:eJTZqMhUDH A.1776@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi,
    >
    > Is there any way to implement an interface as a static method in C#? If
    > not, is this a bug?
    >
    > Micheal.
    >
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: implementing interfaces with static methods

      Patrick Steele [MVP] <patrick@mvps.o rg> wrote:[color=blue]
      > No, you can't do it. And no, it's not a bug. Since an interface is
      > implemented by an instance of an object, how could you have a static
      > member of an instance? :)[/color]

      The one argument I've seen for this is that it would give compile-time
      safety for a member which would then be called with reflection. As in,
      it would prevent people from thinking they'd implemented everything
      they needed to for a certain type of plug-in, when actually they'd
      forgotten a static method which was required. The same could be said of
      constructors.

      My opinion on whether or not that would be a good idea varies quite a
      lot :)

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Chris Capel

        #4
        Re: implementing interfaces with static methods

        Hmm. It seems you're right. Don't see why it should be so.

        Chris

        "100" <100@100.com> wrote in message
        news:#L5SafhUDH A.2228@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Hi Chris,
        >
        > I'm not sure you can use *static* modifier in an interface declaration.
        >
        > B\rgds
        > 100
        >[/color]


        Comment

        • Chris Capel

          #5
          Re: implementing interfaces with static methods

          I'm not sure I follow you here...

          Chris
          [color=blue]
          > The one argument I've seen for this is that it would give compile-time
          > safety for a member which would then be called with reflection. As in,
          > it would prevent people from thinking they'd implemented everything
          > they needed to for a certain type of plug-in, when actually they'd
          > forgotten a static method which was required. The same could be said of
          > constructors.
          >
          > My opinion on whether or not that would be a good idea varies quite a
          > lot :)
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet/
          > If replying to the group, please do not mail me too[/color]


          Comment

          • 100

            #6
            Re: implementing interfaces with static methods

            Hi Chris,

            The problem is that c# (I don't know about CLR) doesn't have support of the
            full conception of meta-classes.
            It support this idea partially. When we declare a class we actually decalre
            two types: one is the given class and the second is the meta-class for the
            given class. The meta-class' members are all members declared as static as
            well as all construcotrs. The one single object of this meta-class type is
            created when the class gets loaded (static constractors is executed which is
            evidence of existance of instance of the meta-class) and the name of this
            objects is the name of the class itself.
            So, calling a static method ClassA.X() should be considered as calling the
            method X of the singleton instance of the meta-class of the ClassA.
            Creating objects of given class obj = new ClassA(...) can considered as a
            short-cut for obj = ClassA.CreateIn stance(....) where CreateInstance methods
            are all constructors of a class.
            What we miss here is a type *refrence to a meta-class*. Since c# doesn't
            support defining variables of type *reference to a meta-class* having
            virtual static methods, static interfaces, etc doesn't make any sense.

            For instance Delphi is a language, which supports meta-class conception. In
            Delphi we can declare variables of type *reference to a meta-class* that's
            why we have virtual constructors for example.

            So, I think that this is more conceptual rather than technical issue. I
            believe that CLR has the whole inforamtion and structures internaly to make
            having virtual constructors, virtual satic members, static interfaces, etc
            posible.
            B\rgds
            100
            ----- Original Message -----
            From: "Chris Capel" <chris@nowhere. com>
            Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
            Sent: Thursday, July 24, 2003 3:37 PM
            Subject: Re: implementing interfaces with static methods

            [color=blue]
            > Hmm. It seems you're right. Don't see why it should be so.
            >
            > Chris
            >
            > "100" <100@100.com> wrote in message
            > news:#L5SafhUDH A.2228@TK2MSFTN GP12.phx.gbl...[color=green]
            > > Hi Chris,
            > >
            > > I'm not sure you can use *static* modifier in an interface declaration.
            > >
            > > B\rgds
            > > 100
            > >[/color]
            >
            >[/color]


            Comment

            • Javier Estrada

              #7
              Re: implementing interfaces with static methods

              "The problem is that c# (I don't know about CLR) doesn't
              have support of the full conception of meta-classes."

              The CLR supports it. It can be done in ILASM--I haven't
              found a sample, but according to the only book on ILASM
              that I've read, it can be done.

              Regards,

              Javier

              Comment

              • Jon Skeet

                #8
                Re: implementing interfaces with static methods

                Javier Estrada <ljestrada@hotm ail.com> wrote:[color=blue]
                > "The problem is that c# (I don't know about CLR) doesn't
                > have support of the full conception of meta-classes."
                >
                > The CLR supports it. It can be done in ILASM--I haven't
                > found a sample, but according to the only book on ILASM
                > that I've read, it can be done.[/color]

                Hmm... I've just modified the IL generated from a normal interface to
                make a method static, ie

                ..method public hidebysig newslot static abstract virtual
                void DoSomething() cil managed
                {
                } // end of method Foo::DoSomethin g

                and compiling against it, it's as if the method doesn't exist.

                Note that the spec (ECMA 335) does say (in section 11, partition 1):

                <quote>
                CLS-compliant interfaces shall not define static methods, nor shall
                they define fields (see
                clause 8.9.4).
                </quote>

                Then again, in 8.9.4 it says that an interface *may* define a static
                method (presumably if it doesn't mind not being CLS-compliant) but it
                doesn't go into details of what that really means.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                Working...