What good is Explicit Interface Implementation?

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

    #1

    What good is Explicit Interface Implementation?

    I just read the section (and did the exercise) in the C#
    Step by Step book that covers Explict Interface
    Implementation (where you specify in the method
    implementation the specific interface that you are
    implementing in the class.

    Other than to resolve the problem that arises when a class
    implements two interfaces with the same method signature,
    what good is it?

    The book says that using EII makes the method effectively
    private to the class, but the example cited doesn't agree
    with the sample program provided. The example says that a
    method, once implemented using the "explicit" syntax must
    be called using a cast.

    IdentifierToken token = new IdentifierToken ();
    token.Accept(vi sitor); // won't complile Accept not
    accessible

    instead...

    ((IVisitable)to ken).Accept(vis itor); // major tacky

    IdentifierToken implements the IVisitable interface (which
    includes Accept() ) using the explicit syntax...

    IVisitable.Acce pt(IVisitor visitor) not...
    Public Accept(IVisitor visitor)

    However the sample program has explicitly implemented
    methods, and no cast is used to call them.

    So what is EII good for and how does it work?
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: What good is Explicit Interface Implementation?

    Steve,

    Explicit interface implementation is used when you want to implement an
    interface but you don't necessarily want to expose those methods on the
    interface as part of the class's methods. For example, you might have a
    control which needs to filter all windows messages coming into the
    application. You would need to implement IMessageFilter for that. With
    explicit interface implementation, you can implement the interface, but not
    expose the PreProcessMessa ge method publically on your control (as it really
    has no use there). This is where explicit interface implementation comes in
    handy.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Steve W." <bigbopmomo@hot mail.com> wrote in message
    news:1e2401c372 fa$095b8890$a40 1280a@phx.gbl.. .[color=blue]
    > I just read the section (and did the exercise) in the C#
    > Step by Step book that covers Explict Interface
    > Implementation (where you specify in the method
    > implementation the specific interface that you are
    > implementing in the class.
    >
    > Other than to resolve the problem that arises when a class
    > implements two interfaces with the same method signature,
    > what good is it?
    >
    > The book says that using EII makes the method effectively
    > private to the class, but the example cited doesn't agree
    > with the sample program provided. The example says that a
    > method, once implemented using the "explicit" syntax must
    > be called using a cast.
    >
    > IdentifierToken token = new IdentifierToken ();
    > token.Accept(vi sitor); // won't complile Accept not
    > accessible
    >
    > instead...
    >
    > ((IVisitable)to ken).Accept(vis itor); // major tacky
    >
    > IdentifierToken implements the IVisitable interface (which
    > includes Accept() ) using the explicit syntax...
    >
    > IVisitable.Acce pt(IVisitor visitor) not...
    > Public Accept(IVisitor visitor)
    >
    > However the sample program has explicitly implemented
    > methods, and no cast is used to call them.
    >
    > So what is EII good for and how does it work?[/color]


    Comment

    • News VS.NET \( MS ILM \)

      #3
      Re: What good is Explicit Interface Implementation?

      Nicholas

      Good example and thank you
      If you look at what Jon Skeet said ( he does not like it )
      how can Mr. Skeet do your good example her without EII

      Thanks again.


      "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
      in message news:%23nFENrvc DHA.1204@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > Steve,
      >
      > Explicit interface implementation is used when you want to implement[/color]
      an[color=blue]
      > interface but you don't necessarily want to expose those methods on the
      > interface as part of the class's methods. For example, you might have a
      > control which needs to filter all windows messages coming into the
      > application. You would need to implement IMessageFilter for that. With
      > explicit interface implementation, you can implement the interface, but[/color]
      not[color=blue]
      > expose the PreProcessMessa ge method publically on your control (as it[/color]
      really[color=blue]
      > has no use there). This is where explicit interface implementation comes[/color]
      in[color=blue]
      > handy.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nicholas.paldin o@exisconsultin g.com
      >
      > "Steve W." <bigbopmomo@hot mail.com> wrote in message
      > news:1e2401c372 fa$095b8890$a40 1280a@phx.gbl.. .[color=green]
      > > I just read the section (and did the exercise) in the C#
      > > Step by Step book that covers Explict Interface
      > > Implementation (where you specify in the method
      > > implementation the specific interface that you are
      > > implementing in the class.
      > >
      > > Other than to resolve the problem that arises when a class
      > > implements two interfaces with the same method signature,
      > > what good is it?
      > >
      > > The book says that using EII makes the method effectively
      > > private to the class, but the example cited doesn't agree
      > > with the sample program provided. The example says that a
      > > method, once implemented using the "explicit" syntax must
      > > be called using a cast.
      > >
      > > IdentifierToken token = new IdentifierToken ();
      > > token.Accept(vi sitor); // won't complile Accept not
      > > accessible
      > >
      > > instead...
      > >
      > > ((IVisitable)to ken).Accept(vis itor); // major tacky
      > >
      > > IdentifierToken implements the IVisitable interface (which
      > > includes Accept() ) using the explicit syntax...
      > >
      > > IVisitable.Acce pt(IVisitor visitor) not...
      > > Public Accept(IVisitor visitor)
      > >
      > > However the sample program has explicitly implemented
      > > methods, and no cast is used to call them.
      > >
      > > So what is EII good for and how does it work?[/color]
      >
      >[/color]


      Comment

      • 100

        #4
        Re: What good is Explicit Interface Implementation?

        As an example of when using EII comes handy could be when you want to
        implemet typed list class which support IList interface

        class MyClass
        {
        }

        class MyList : IList
        {
        }

        You can do that like:

        class MyList : IList
        {
        public int Add(MyClass obj)
        {
        //save the reference in a list and return the index
        }

        int IList.Add(objec t obj)
        {
        //this will throw execption if the class is added using IList and
        the obj is not of MyClass type.
        //You can make your own typecheck and throw your own exception
        return this.Add((MyCla ss)obj);
        }
        }

        if you don't do EII the class will expose method
        int Add(object obj) which doesn't make sence in typed list class.
        MyList has to accept only objects of MyClass

        HTH
        B\rgds
        100

        "Steve W." <bigbopmomo@hot mail.com> wrote in message
        news:1e2401c372 fa$095b8890$a40 1280a@phx.gbl.. .[color=blue]
        > I just read the section (and did the exercise) in the C#
        > Step by Step book that covers Explict Interface
        > Implementation (where you specify in the method
        > implementation the specific interface that you are
        > implementing in the class.
        >
        > Other than to resolve the problem that arises when a class
        > implements two interfaces with the same method signature,
        > what good is it?
        >
        > The book says that using EII makes the method effectively
        > private to the class, but the example cited doesn't agree
        > with the sample program provided. The example says that a
        > method, once implemented using the "explicit" syntax must
        > be called using a cast.
        >
        > IdentifierToken token = new IdentifierToken ();
        > token.Accept(vi sitor); // won't complile Accept not
        > accessible
        >
        > instead...
        >
        > ((IVisitable)to ken).Accept(vis itor); // major tacky
        >
        > IdentifierToken implements the IVisitable interface (which
        > includes Accept() ) using the explicit syntax...
        >
        > IVisitable.Acce pt(IVisitor visitor) not...
        > Public Accept(IVisitor visitor)
        >
        > However the sample program has explicitly implemented
        > methods, and no cast is used to call them.
        >
        > So what is EII good for and how does it work?[/color]


        Comment

        • Joe Mayo

          #5
          Re: What good is Explicit Interface Implementation?

          I use it every once in a while. In fact, I used it yesterday, because I had
          to. The project I'm working on uses Interfaces extensively. We have a lot
          of objects that must implement the same set of methods and set apart in
          multiple projects. Interfaces help us make all the objects behave themselves
          and allow us to avoid circularities caused by cross project composition.

          Anyway, yesterday morning I did a Get Latest and found that someone on the
          team added a Name property with only a get accessor to Interface IFoo. I
          already had a Name property in my type with both get and set accessors, so
          the compile died because I was setting my name property, which would not
          have otherwise have been detected, potentially causing runtime problems
          later. Anyway, the fix was to implement the IFoo.Name property with the get
          and no set accessor. Since we assign instantiated objects to variables of
          the interface type (rather than casting), the proper method gets called,
          preserving the semantic integrity of the operation.

          Joe
          --
          Learn C# with step-by-step tutorials and practical examples. Start your first program, explore lists and parsing, and understand how .NET runs your code.


          "Steve W." <bigbopmomo@hot mail.com> wrote in message
          news:1e2401c372 fa$095b8890$a40 1280a@phx.gbl.. .[color=blue]
          >
          > Other than to resolve the problem that arises when a class
          > implements two interfaces with the same method signature,
          > what good is it?[/color]


          Comment

          • News VS.NET \( MS ILM \)

            #6
            Re: What good is Explicit Interface Implementation?

            Joe,
            Can you please provide us with a small sample of code to illustrate your fix
            Please provide before and after code
            Thank you for your input


            "Joe Mayo" <jmayo@ddiieess ppaammeerrssddi iee.com> wrote in message
            news:%23MIOrUxc DHA.1656@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > I use it every once in a while. In fact, I used it yesterday, because I[/color]
            had[color=blue]
            > to. The project I'm working on uses Interfaces extensively. We have a lot
            > of objects that must implement the same set of methods and set apart in
            > multiple projects. Interfaces help us make all the objects behave[/color]
            themselves[color=blue]
            > and allow us to avoid circularities caused by cross project composition.
            >
            > Anyway, yesterday morning I did a Get Latest and found that someone on the
            > team added a Name property with only a get accessor to Interface IFoo. I
            > already had a Name property in my type with both get and set accessors, so
            > the compile died because I was setting my name property, which would not
            > have otherwise have been detected, potentially causing runtime problems
            > later. Anyway, the fix was to implement the IFoo.Name property with the[/color]
            get[color=blue]
            > and no set accessor. Since we assign instantiated objects to variables of
            > the interface type (rather than casting), the proper method gets called,
            > preserving the semantic integrity of the operation.
            >
            > Joe
            > --
            > http://www.csharp-station.com
            >
            > "Steve W." <bigbopmomo@hot mail.com> wrote in message
            > news:1e2401c372 fa$095b8890$a40 1280a@phx.gbl.. .[color=green]
            > >
            > > Other than to resolve the problem that arises when a class
            > > implements two interfaces with the same method signature,
            > > what good is it?[/color]
            >
            >[/color]


            Comment

            • 100

              #7
              Re: What good is Explicit Interface Implementation?

              Hi Jon,[color=blue]
              > 100 <100@100.com> wrote:
              >
              > <snip>
              >[color=green]
              > > if you don't do EII the class will expose method
              > > int Add(object obj) which doesn't make sence in typed list class.
              > > MyList has to accept only objects of MyClass[/color]
              >
              > It still exposes int Add(object obj) - it just hides it very slightly
              > so that only code looking at it in a certain kind of way can see it.
              >[/color]

              Sometimes I have to implement a standard interface just because the
              framework needs it.
              As in my example I have designed the list to hold *only* MyClass objects. I
              have to pass the objects to some library class method, which expects IList,
              though. Hiding interface implementation tells the users of my class that
              they shouldn't use IList members in normal circumstances. They still can,
              though.
              Another example is *Control.Contro lCollection*. The designer of this class
              decided to implement IList (maybe there is some reason to do so) , but
              doesn't want to implement IList.Insert method. S/he hid all or part of
              IList's methods and added new method *SetChildIndex* . Anyway because the
              objects can be cast to IList IList.Insert throws an excpetion. How ever when
              I needed to insert a control at index 0 I saw that there is no such a method
              exposed by the class. However, I tried to cast the collection to IList, but
              I already new that it won't do.

              Anyway, I believe that the main reason for EII is to make posible to
              implement different interfaces, which have methods with the same prototype.
              All other usages is just tricks that *might* make the code more clear.


              Comment

              • Joe Mayo

                #8
                Re: What good is Explicit Interface Implementation?

                I can't post client code, but here's the scenario.

                *** Initial Code

                using System;

                interface IMyInterface
                {
                }

                class MyBase
                {
                private string m_name;

                public string Name
                {
                get { return m_name; }
                set { m_name = value; }
                }
                }

                class MyClass : MyBase, IMyInterface
                {
                static void Main()
                {
                MyClass myCl = new MyClass();
                myCl.Run();

                Console.WriteLi ne("myClass.Nam e: {0}", myCl.Name);
                Console.ReadLin e();
                }

                public void Run()
                {
                this.Name = "Joe";
                }
                }

                Notice that I have a property that is being set on an instance of this
                MyClass. For other purposes, MyClass implements IMyInterface.

                Now, someone decides to change IMyInterface like this:

                *** Intermediate Code

                interface IMyInterface
                {
                string Name
                {
                get;
                }
                // plus some other new stuff.
                }

                class MyClass : MyBase, IMyInterface
                {
                // other implementation elided for clarity

                public void Run()
                {
                this.Name = "Joe"; // compiler error
                }
                public string Name
                {
                get { return "Mayo, Joe"; }
                }

                Although MyClass implements Name by virtue of inheriting it from MyBase,
                IMyInterface specifies other new members that I must include. So, I do a
                thorough review of MyClass and make sure that it implements IMyInterface
                properly (in VS.NET retype the interface name and press the Tab key),
                including the IMyInterface version of Name. This generates a compiler error
                on MyClass.Name because it is hiding MyBase.Name and only implements a get.

                So, I do an explicit implementation of Name in MyClass that allows me to use
                MyClass.Name and IMyInterface name in the proper contexts and preserve the
                semantics of my type.

                *** Final Code

                using System;

                interface IMyInterface
                {
                string Name
                {
                get;
                }
                }

                class MyBase
                {
                private string m_name;

                public string Name
                {
                get { return m_name; }
                set { m_name = value; }
                }
                }

                class MyClass : MyBase, IMyInterface
                {
                static void Main()
                {
                MyClass myCl = new MyClass();
                myCl.Run();

                IMyInterface myIf = new MyClass();

                Console.WriteLi ne("myCl.Name: {0}", myCl.Name);
                Console.WriteLi ne("myIf.Name: {0}", myIf.Name);
                Console.ReadLin e();
                }

                public void Run()
                {
                this.Name = "Joe";
                }

                string IMyInterface.Na me
                {
                get { return "Mayo, Joe"; }
                }
                }

                And here's the output:

                myCl.Name: Joe
                myIf.Name: Mayo, Joe

                Joe
                --
                Learn C# with step-by-step tutorials and practical examples. Start your first program, explore lists and parsing, and understand how .NET runs your code.


                "News VS.NET ( MS ILM )" <sql_agentman@h otmail.com> wrote in message
                news:OkCI0lxcDH A.568@TK2MSFTNG P11.phx.gbl...[color=blue]
                > Joe,
                > Can you please provide us with a small sample of code to illustrate your[/color]
                fix[color=blue]
                > Please provide before and after code
                > Thank you for your input
                >
                >
                > "Joe Mayo" <jmayo@ddiieess ppaammeerrssddi iee.com> wrote in message
                > news:%23MIOrUxc DHA.1656@TK2MSF TNGP10.phx.gbl. ..[color=green]
                > > I use it every once in a while. In fact, I used it yesterday, because I[/color]
                > had[color=green]
                > > to. The project I'm working on uses Interfaces extensively. We have a[/color][/color]
                lot[color=blue][color=green]
                > > of objects that must implement the same set of methods and set apart in
                > > multiple projects. Interfaces help us make all the objects behave[/color]
                > themselves[color=green]
                > > and allow us to avoid circularities caused by cross project composition.
                > >
                > > Anyway, yesterday morning I did a Get Latest and found that someone on[/color][/color]
                the[color=blue][color=green]
                > > team added a Name property with only a get accessor to Interface IFoo. I
                > > already had a Name property in my type with both get and set accessors,[/color][/color]
                so[color=blue][color=green]
                > > the compile died because I was setting my name property, which would not
                > > have otherwise have been detected, potentially causing runtime problems
                > > later. Anyway, the fix was to implement the IFoo.Name property with the[/color]
                > get[color=green]
                > > and no set accessor. Since we assign instantiated objects to variables[/color][/color]
                of[color=blue][color=green]
                > > the interface type (rather than casting), the proper method gets called,
                > > preserving the semantic integrity of the operation.
                > >
                > > Joe
                > > --
                > > http://www.csharp-station.com
                > >
                > > "Steve W." <bigbopmomo@hot mail.com> wrote in message
                > > news:1e2401c372 fa$095b8890$a40 1280a@phx.gbl.. .[color=darkred]
                > > >
                > > > Other than to resolve the problem that arises when a class
                > > > implements two interfaces with the same method signature,
                > > > what good is it?[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Eric Gunnerson [MS]

                  #9
                  Re: What good is Explicit Interface Implementation?

                  Jon,

                  I tend to agree with you for many interfaces. But if the interface is for
                  something that isn't really the main purpose of the class, you often don't
                  want to make it visible to the user.

                  The Serialize function on ISerializable is a good example. I don't want it
                  cluttering up the user model.

                  --
                  Eric Gunnerson

                  Visit the C# product team at http://www.csharp.net
                  Eric's blog is at http://blogs.gotdotnet.com/ericgu/

                  This posting is provided "AS IS" with no warranties, and confers no rights.
                  "Jon Skeet" <skeet@pobox.co m> wrote in message
                  news:MPG.19c18c 2eedff419398a4a d@news.microsof t.com...[color=blue]
                  > "News VS.NET \( MS ILM \)" <sql_agentman@h otmail.com> wrote:[color=green]
                  > > Good example and thank you
                  > > If you look at what Jon Skeet said ( he does not like it )
                  > > how can Mr. Skeet do your good example her without EII[/color]
                  >
                  > I wouldn't. I believe if you're going to implement an interface, you
                  > just implement it - none of this "Well, I sort of implement it, but
                  > only if you cast to the right thing."
                  >
                  > It doesn't actually stop anyone from doing things, it just makes it
                  > harder to do them due to casting which looks strange.
                  >
                  > --
                  > Jon Skeet - <skeet@pobox.co m>
                  > http://www.pobox.com/~skeet/
                  > If replying to the group, please do not mail me too[/color]


                  Comment

                  • News VS.NET \( MS ILM \)

                    #10
                    Re: What good is Explicit Interface Implementation?

                    Thank you, I don't expect you to post client code, just an example


                    "Joe Mayo" <jmayo@ddiieess ppaammeerrssddi iee.com> wrote in message
                    news:eDJPEOycDH A.456@TK2MSFTNG P10.phx.gbl...[color=blue]
                    > I can't post client code, but here's the scenario.
                    >
                    > *** Initial Code
                    >
                    > using System;
                    >
                    > interface IMyInterface
                    > {
                    > }
                    >
                    > class MyBase
                    > {
                    > private string m_name;
                    >
                    > public string Name
                    > {
                    > get { return m_name; }
                    > set { m_name = value; }
                    > }
                    > }
                    >
                    > class MyClass : MyBase, IMyInterface
                    > {
                    > static void Main()
                    > {
                    > MyClass myCl = new MyClass();
                    > myCl.Run();
                    >
                    > Console.WriteLi ne("myClass.Nam e: {0}", myCl.Name);
                    > Console.ReadLin e();
                    > }
                    >
                    > public void Run()
                    > {
                    > this.Name = "Joe";
                    > }
                    > }
                    >
                    > Notice that I have a property that is being set on an instance of this
                    > MyClass. For other purposes, MyClass implements IMyInterface.
                    >
                    > Now, someone decides to change IMyInterface like this:
                    >
                    > *** Intermediate Code
                    >
                    > interface IMyInterface
                    > {
                    > string Name
                    > {
                    > get;
                    > }
                    > // plus some other new stuff.
                    > }
                    >
                    > class MyClass : MyBase, IMyInterface
                    > {
                    > // other implementation elided for clarity
                    >
                    > public void Run()
                    > {
                    > this.Name = "Joe"; // compiler error
                    > }
                    > public string Name
                    > {
                    > get { return "Mayo, Joe"; }
                    > }
                    >
                    > Although MyClass implements Name by virtue of inheriting it from MyBase,
                    > IMyInterface specifies other new members that I must include. So, I do a
                    > thorough review of MyClass and make sure that it implements IMyInterface
                    > properly (in VS.NET retype the interface name and press the Tab key),
                    > including the IMyInterface version of Name. This generates a compiler[/color]
                    error[color=blue]
                    > on MyClass.Name because it is hiding MyBase.Name and only implements a[/color]
                    get.[color=blue]
                    >
                    > So, I do an explicit implementation of Name in MyClass that allows me to[/color]
                    use[color=blue]
                    > MyClass.Name and IMyInterface name in the proper contexts and preserve the
                    > semantics of my type.
                    >
                    > *** Final Code
                    >
                    > using System;
                    >
                    > interface IMyInterface
                    > {
                    > string Name
                    > {
                    > get;
                    > }
                    > }
                    >
                    > class MyBase
                    > {
                    > private string m_name;
                    >
                    > public string Name
                    > {
                    > get { return m_name; }
                    > set { m_name = value; }
                    > }
                    > }
                    >
                    > class MyClass : MyBase, IMyInterface
                    > {
                    > static void Main()
                    > {
                    > MyClass myCl = new MyClass();
                    > myCl.Run();
                    >
                    > IMyInterface myIf = new MyClass();
                    >
                    > Console.WriteLi ne("myCl.Name: {0}", myCl.Name);
                    > Console.WriteLi ne("myIf.Name: {0}", myIf.Name);
                    > Console.ReadLin e();
                    > }
                    >
                    > public void Run()
                    > {
                    > this.Name = "Joe";
                    > }
                    >
                    > string IMyInterface.Na me
                    > {
                    > get { return "Mayo, Joe"; }
                    > }
                    > }
                    >
                    > And here's the output:
                    >
                    > myCl.Name: Joe
                    > myIf.Name: Mayo, Joe
                    >
                    > Joe
                    > --
                    > http://www.csharp-station.com
                    >
                    > "News VS.NET ( MS ILM )" <sql_agentman@h otmail.com> wrote in message
                    > news:OkCI0lxcDH A.568@TK2MSFTNG P11.phx.gbl...[color=green]
                    > > Joe,
                    > > Can you please provide us with a small sample of code to illustrate your[/color]
                    > fix[color=green]
                    > > Please provide before and after code
                    > > Thank you for your input
                    > >
                    > >
                    > > "Joe Mayo" <jmayo@ddiieess ppaammeerrssddi iee.com> wrote in message
                    > > news:%23MIOrUxc DHA.1656@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
                    > > > I use it every once in a while. In fact, I used it yesterday, because[/color][/color][/color]
                    I[color=blue][color=green]
                    > > had[color=darkred]
                    > > > to. The project I'm working on uses Interfaces extensively. We have a[/color][/color]
                    > lot[color=green][color=darkred]
                    > > > of objects that must implement the same set of methods and set apart[/color][/color][/color]
                    in[color=blue][color=green][color=darkred]
                    > > > multiple projects. Interfaces help us make all the objects behave[/color]
                    > > themselves[color=darkred]
                    > > > and allow us to avoid circularities caused by cross project[/color][/color][/color]
                    composition.[color=blue][color=green][color=darkred]
                    > > >
                    > > > Anyway, yesterday morning I did a Get Latest and found that someone on[/color][/color]
                    > the[color=green][color=darkred]
                    > > > team added a Name property with only a get accessor to Interface IFoo.[/color][/color][/color]
                    I[color=blue][color=green][color=darkred]
                    > > > already had a Name property in my type with both get and set[/color][/color][/color]
                    accessors,[color=blue]
                    > so[color=green][color=darkred]
                    > > > the compile died because I was setting my name property, which would[/color][/color][/color]
                    not[color=blue][color=green][color=darkred]
                    > > > have otherwise have been detected, potentially causing runtime[/color][/color][/color]
                    problems[color=blue][color=green][color=darkred]
                    > > > later. Anyway, the fix was to implement the IFoo.Name property with[/color][/color][/color]
                    the[color=blue][color=green]
                    > > get[color=darkred]
                    > > > and no set accessor. Since we assign instantiated objects to variables[/color][/color]
                    > of[color=green][color=darkred]
                    > > > the interface type (rather than casting), the proper method gets[/color][/color][/color]
                    called,[color=blue][color=green][color=darkred]
                    > > > preserving the semantic integrity of the operation.
                    > > >
                    > > > Joe
                    > > > --
                    > > > http://www.csharp-station.com
                    > > >
                    > > > "Steve W." <bigbopmomo@hot mail.com> wrote in message
                    > > > news:1e2401c372 fa$095b8890$a40 1280a@phx.gbl.. .
                    > > > >
                    > > > > Other than to resolve the problem that arises when a class
                    > > > > implements two interfaces with the same method signature,
                    > > > > what good is it?
                    > > >
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Jon Skeet

                      #11
                      Re: What good is Explicit Interface Implementation?

                      100 <100@100.com> wrote:

                      <snip>
                      [color=blue]
                      > Anyway, I believe that the main reason for EII is to make posible to
                      > implement different interfaces, which have methods with the same
                      > prototype.[/color]

                      Agreed. Hopefully that's a very rare event though.
                      [color=blue]
                      > All other usages is just tricks that *might* make the code more clear.[/color]

                      And in my view it makes the code *less* clear. Again, just a matter of
                      taste.

                      --
                      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

                      • 100

                        #12
                        Re: What good is Explicit Interface Implementation?

                        Hi Steve,
                        I think that EII exist only for making posible implementing different
                        interfaces having methods with the same prototype. In this case making more
                        than one of them visible will lead to ambiguities. Furthermore, if you make
                        one of them visible the user of your class won't know part of which
                        interface is the method s/he is calling. Obviously they have different
                        implementation.

                        B\rgds
                        100

                        "Steve W." <bigbopmomo@hot mail.com> wrote in message
                        news:1e4401c373 44$5af93e00$a00 1280a@phx.gbl.. .[color=blue]
                        > Many thanks to all responders. It's clearer now, but I do
                        > agree with Jon that being able to ultimately call the
                        > explicitly implemented method through casting is very
                        > obscure. I see that multiple interfaces with the same
                        > method signature needs a solution, but why can't we just
                        > explicitly control the access of the implemented method if
                        > that needs to be done?
                        >
                        > Steve W.[color=green]
                        > >-----Original Message-----
                        > >I just read the section (and did the exercise) in the C#
                        > >Step by Step book that covers Explict Interface
                        > >Implementati on (where you specify in the method
                        > >implementati on the specific interface that you are
                        > >implementing in the class.
                        > >
                        > >Other than to resolve the problem that arises when a[/color]
                        > class[color=green]
                        > >implements two interfaces with the same method signature,
                        > >what good is it?
                        > >
                        > >The book says that using EII makes the method effectively
                        > >private to the class, but the example cited doesn't agree
                        > >with the sample program provided. The example says that[/color]
                        > a[color=green]
                        > >method, once implemented using the "explicit" syntax must
                        > >be called using a cast.
                        > >
                        > >IdentifierToke n token = new IdentifierToken ();
                        > >token.Accept(v isitor); // won't complile Accept not
                        > >accessible
                        > >
                        > >instead...
                        > >
                        > >((IVisitable)t oken).Accept(vi sitor); // major tacky
                        > >
                        > >IdentifierToke n implements the IVisitable interface[/color]
                        > (which[color=green]
                        > >includes Accept() ) using the explicit syntax...
                        > >
                        > >IVisitable.Acc ept(IVisitor visitor) not...
                        > >Public Accept(IVisitor visitor)
                        > >
                        > >However the sample program has explicitly implemented
                        > >methods, and no cast is used to call them.
                        > >
                        > >So what is EII good for and how does it work?
                        > >.
                        > >[/color][/color]


                        Comment

                        • Eric Gunnerson [MS]

                          #13
                          Re: What good is Explicit Interface Implementation?

                          "Jon Skeet" <skeet@pobox.co m> wrote in message
                          news:MPG.19c231 fadf951f4498a4b 0@news.microsof t.com...[color=blue]
                          > Eric Gunnerson [MS] <ericgu@online. microsoft.com> wrote:[color=green]
                          > > I tend to agree with you for many interfaces. But if the interface is[/color][/color]
                          for[color=blue][color=green]
                          > > something that isn't really the main purpose of the class, you often[/color][/color]
                          don't[color=blue][color=green]
                          > > want to make it visible to the user.
                          > >
                          > > The Serialize function on ISerializable is a good example. I don't want[/color][/color]
                          it[color=blue][color=green]
                          > > cluttering up the user model.[/color]
                          >
                          > But my point is that it still *is* visible to the user, because the
                          > fact that it implements ISerializable is visible.[/color]

                          True, though when users look through a class to find what methods to call,
                          they probably don't look at the interfaces it implements. Or at least I
                          don't.

                          But it is really a convenience mechanism - it doesn't prevent access, it
                          just reduces clutter.
                          [color=blue]
                          >
                          > It just feels really clunky to me, I'm afraid - although I can see
                          > where it's needed for name collisions.[/color]




                          --
                          Eric Gunnerson

                          Visit the C# product team at http://www.csharp.net
                          Eric's blog is at http://blogs.gotdotnet.com/ericgu/

                          This posting is provided "AS IS" with no warranties, and confers no rights.


                          Comment

                          Working...