Interface Name Collisions

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

    #1

    Interface Name Collisions

    Hey,

    I was trying some code with the .Net framework 1.1, and I found that the
    Interface name collisions is still not resolved in that version of the .net
    framework.
    I am refering to that piece of code which i took from the book of Inside C#
    (Archer).

    using System;

    interface ISerializable
    {
    void SaveData();
    }

    interface IDataStore
    {
    void SaveData();
    }

    class Test : ISerializable, IDataStore
    {
    public void SaveData()
    {
    Console.WriteLi ne("Test.SaveDa ta called");
    }
    }

    class NameCollisions1 App
    {
    public static void Main()
    {
    Test test = new Test();

    Console.WriteLi ne("Calling Test.SaveData() ");
    test.SaveData() ;
    }
    }

    the code is still compiling, although logically it shouldn't.
    So My question is Why this is not resolved ?
    and is there an apparent benefit from this capability that i am not aware
    of?

    regards


  • Aravind C

    #2
    Re: Interface Name Collisions

    Hi Walid,

    If explicit interfaces are not used, the same SaveData()
    implementation is used for both the interfaces. I think this feature
    is in place in order to get a default implementation
    to service both the interfaces - This can be used when
    ISerializable.S aveData() and IDataStore.Save Data() intend to represent
    the same semantics.

    If teh SaveData for each of the interfaces intend to convey different
    semantics,
    you may like to actually use explicit interfaces to define
    separate implementations for each of the SaveData() methods.

    class Test : ISerializable, IDataStore
    {
    void ISerialzable.Sa veData()
    {
    // Implement ISerializable.S aveData
    }
    void IDataStore.Save Data()
    {
    // Implement IDataStore.Save Data
    }

    }

    Regards,
    Aravind C


    "Walid" <wkoleilat@hotm ail.com> wrote in message
    news:uhc4q1opDH A.2588@tk2msftn gp13.phx.gbl...[color=blue]
    > Hey,
    >
    > I was trying some code with the .Net framework 1.1, and I found that the
    > Interface name collisions is still not resolved in that version of the[/color]
    ..net[color=blue]
    > framework.
    > I am refering to that piece of code which i took from the book of Inside[/color]
    C#[color=blue]
    > (Archer).
    >
    > using System;
    >
    > interface ISerializable
    > {
    > void SaveData();
    > }
    >
    > interface IDataStore
    > {
    > void SaveData();
    > }
    >
    > class Test : ISerializable, IDataStore
    > {
    > public void SaveData()
    > {
    > Console.WriteLi ne("Test.SaveDa ta called");
    > }
    > }
    >
    > class NameCollisions1 App
    > {
    > public static void Main()
    > {
    > Test test = new Test();
    >
    > Console.WriteLi ne("Calling Test.SaveData() ");
    > test.SaveData() ;
    > }
    > }
    >
    > the code is still compiling, although logically it shouldn't.
    > So My question is Why this is not resolved ?
    > and is there an apparent benefit from this capability that i am not aware
    > of?
    >
    > regards
    >
    >[/color]


    Comment

    • Walid

      #3
      Re: Interface Name Collisions

      Hi Aravind C,

      Thnx But why this is allowed to compile ?
      (refering to the non referenced method name)
      shouldn't be a compile error?
      regards
      "Aravind C" <aravindcorera@ nospam.hotmail. com> wrote in message
      news:eW%23yzmpp DHA.2592@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > Hi Walid,
      >
      > If explicit interfaces are not used, the same SaveData()
      > implementation is used for both the interfaces. I think this feature
      > is in place in order to get a default implementation
      > to service both the interfaces - This can be used when
      > ISerializable.S aveData() and IDataStore.Save Data() intend to represent
      > the same semantics.
      >
      > If teh SaveData for each of the interfaces intend to convey different
      > semantics,
      > you may like to actually use explicit interfaces to define
      > separate implementations for each of the SaveData() methods.
      >
      > class Test : ISerializable, IDataStore
      > {
      > void ISerialzable.Sa veData()
      > {
      > // Implement ISerializable.S aveData
      > }
      > void IDataStore.Save Data()
      > {
      > // Implement IDataStore.Save Data
      > }
      >
      > }
      >
      > Regards,
      > Aravind C
      >
      >
      > "Walid" <wkoleilat@hotm ail.com> wrote in message
      > news:uhc4q1opDH A.2588@tk2msftn gp13.phx.gbl...[color=green]
      > > Hey,
      > >
      > > I was trying some code with the .Net framework 1.1, and I found that the
      > > Interface name collisions is still not resolved in that version of the[/color]
      > .net[color=green]
      > > framework.
      > > I am refering to that piece of code which i took from the book of Inside[/color]
      > C#[color=green]
      > > (Archer).
      > >
      > > using System;
      > >
      > > interface ISerializable
      > > {
      > > void SaveData();
      > > }
      > >
      > > interface IDataStore
      > > {
      > > void SaveData();
      > > }
      > >
      > > class Test : ISerializable, IDataStore
      > > {
      > > public void SaveData()
      > > {
      > > Console.WriteLi ne("Test.SaveDa ta called");
      > > }
      > > }
      > >
      > > class NameCollisions1 App
      > > {
      > > public static void Main()
      > > {
      > > Test test = new Test();
      > >
      > > Console.WriteLi ne("Calling Test.SaveData() ");
      > > test.SaveData() ;
      > > }
      > > }
      > >
      > > the code is still compiling, although logically it shouldn't.
      > > So My question is Why this is not resolved ?
      > > and is there an apparent benefit from this capability that i am not[/color][/color]
      aware[color=blue][color=green]
      > > of?
      > >
      > > regards
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Interface Name Collisions

        Walid <wkoleilat@hotm ail.com> wrote:[color=blue]
        > Thnx But why this is allowed to compile ?
        > (refering to the non referenced method name)
        > shouldn't be a compile error?[/color]

        No. In some cases it may be exactly what you want. Is there something
        in the spec which makes you think it *should* be a compile error?

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

        • Walid

          #5
          Re: Interface Name Collisions

          How can be exactly what the client wants since he doesn't know which method
          has been called?

          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1a180d 129a31ab9d989a5 4@msnews.micros oft.com...[color=blue]
          > Walid <wkoleilat@hotm ail.com> wrote:[color=green]
          > > Thnx But why this is allowed to compile ?
          > > (refering to the non referenced method name)
          > > shouldn't be a compile error?[/color]
          >
          > No. In some cases it may be exactly what you want. Is there something
          > in the spec which makes you think it *should* be a compile error?
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Daniel O'Connell

            #6
            Re: Interface Name Collisions


            "Walid" <wkoleilat@hotm ail.com> wrote in message
            news:OMv543ppDH A.1672@TK2MSFTN GP09.phx.gbl...[color=blue]
            > How can be exactly what the client wants since he doesn't know which[/color]
            method[color=blue]
            > has been called?
            >[/color]
            Casting.
            The client should, if it is concerned with which version of the method is
            called, cast to the right interface.
            using IDataStore, ISerializable like the earlier examples were:

            class Class : IDataStore, ISerializable
            {
            public void SaveData()
            {

            }

            void IDataStore.Save Data()
            {

            }

            void ISerializable.S aveData()
            {
            }
            }
            IDataStore isd = (IDataStore)Cla ss;
            ISerializable iserial = ISerializable(C lass);

            ids.SaveData() // calls IDataStore.Save ()
            iserial.SavData () // calls ISerializable.S ave();
            Class.SaveData( ) // calls the public method save data, be it an interface
            method or just a plain public method.[color=blue]
            > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            > news:MPG.1a180d 129a31ab9d989a5 4@msnews.micros oft.com...[color=green]
            > > Walid <wkoleilat@hotm ail.com> wrote:[color=darkred]
            > > > Thnx But why this is allowed to compile ?
            > > > (refering to the non referenced method name)
            > > > shouldn't be a compile error?[/color]
            > >
            > > No. In some cases it may be exactly what you want. Is there something
            > > in the spec which makes you think it *should* be a compile error?
            > >
            > > --
            > > Jon Skeet - <skeet@pobox.co m>
            > > http://www.pobox.com/~skeet
            > > If replying to the group, please do not mail me too[/color]
            >
            >[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Interface Name Collisions

              Walid <wkoleilat@hotm ail.com> wrote:[color=blue]
              > How can be exactly what the client wants since he doesn't know which method
              > has been called?[/color]

              If there's only one method, and that method satisfies the contract of
              the interface, then why would he care? Why should someone have to
              specify two methods which do the same thing, just to satisfy two
              different interfaces which use the same method name?

              Only if the interface names clash *and* the semantics of the interfaces
              require different implementations is there a problem, which is
              "solved" by explicit interface implementation. (Personally I'm not a
              fan of EII in the first place, but there we go. I can see how it's
              useful for solving this rare problem, but it seems it's often used when
              it doesn't need to be.)

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