Using interface and dispose

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

    Using interface and dispose

    Hello!

    I know that the block of code below is the same as using the using clause.
    I must have this kind of text because the question is about the statement
    ((IDisposable)r eader).Dispose( );

    Now to my question:
    If I use this statement ((IDisposable)r eader).Dispose( );
    or reader.Dispose( );
    must mean exactly the same thing.
    It's the method Dispose in the TextReader class that is called here because
    it's not virtual.


    {
    TextReader reader = new StreamReader("s ome filename";
    try
    {
    string line;
    while((line = reader.ReadLine ()) != null)
    {
    Console.WrileLi ne(line);
    }
    }
    finally
    {
    if (reader != null)
    {
    ((IDisposable)r edaer).Dispose( );
    }
    }
    }

    //Tony


  • Jon Skeet [C# MVP]

    #2
    Re: Using interface and dispose

    On Jun 17, 12:14 pm, "Tony" <johansson.ande rs...@telia.com wrote:
    So which Dispose in which class is called.
    I far as I understand it must be in the TextReader class.
    In this particular case, yes.

    However, suppose you had:

    public class OddTextReader : TextReader, IDisposable
    {
    // Abstract methods etc filled in

    void IDisposable.Dis pose()
    {
    // Some stuff here
    }
    }

    then:

    using (TextReader reader = new OddTextReader() )
    {
    ...
    }

    would call the OddTextReader's explicit interface implementation of
    Dispose.

    Jon

    Comment

    • Tony

      #3
      Re: Using interface and dispose

      Hello!

      I tested you example and it was the Dispose of OddTextReader that was
      called.
      I would understand it if this Dispose had been declared as virtual in
      TextReader.
      Here is the same as your example. Here I get the same strange result that I
      don't understand.
      When test is called as an interface it's test in MyDerived that is called in
      spite of
      having this method Test as public void Test(...) not using virtual
      If I replace ITest with MyBase then Test in the MyBase is called which is
      correct and understandable.

      So can you explain why Test in MyDerived class is called. It should be Test
      in MyBase according to my knowledge.

      ITest test = new MyDerived();
      test.Test();

      interface ITest
      { void Test() }

      public class MyBase
      { public void Test() {} }

      public class MyDerived : MyBase, ITest
      { public void Test() {} }

      //Tony

      "Jon Skeet [C# MVP]" <skeet@pobox.co mskrev i meddelandet
      news:8227789b-98d6-49c1-b1a5-8c8aeece8277@2g 2000hsn.googleg roups.com...
      On Jun 17, 12:14 pm, "Tony" <johansson.ande rs...@telia.com wrote:
      So which Dispose in which class is called.
      I far as I understand it must be in the TextReader class.
      >
      In this particular case, yes.
      >
      However, suppose you had:
      >
      public class OddTextReader : TextReader, IDisposable
      {
      // Abstract methods etc filled in
      >
      void IDisposable.Dis pose()
      {
      // Some stuff here
      }
      }
      >
      then:
      >
      using (TextReader reader = new OddTextReader() )
      {
      ...
      }
      >
      would call the OddTextReader's explicit interface implementation of
      Dispose.
      >
      Jon

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Using interface and dispose

        On Jun 17, 2:38 pm, "Tony" <johansson.ande rs...@telia.com wrote:

        <snip>
        So can you explain why Test in MyDerived class is called. It should be Test
        in MyBase according to my knowledge.
        Well, you're reimplementing ITest (although note that you're doing it
        in a different way to my example - you're not using explicit interface
        implementation) .

        Sections 13.4.5 and 13.4.6 of the C# 3 spec go into the details.

        Jon

        Comment

        • Tony

          #5
          Re: Using interface and dispose

          Hello!

          Your example work the same even if I implement this Dispose implicit meaning
          that it's the Dispose in OddTextReader that is called.
          The reason for this accoring to my knowledge is that
          the call to Dispose is beibng made as a being a Disposable.

          public class OddTextReader : TextReader, IDisposable
          {
          // Abstract methods etc filled in

          void IDisposable.Dis pose()
          {
          // Some stuff here
          }
          }

          then:

          using (TextReader reader = new OddTextReader() )
          {
          ((IDisposable)r eader).Dispose( );
          ...
          }

          would call the OddTextReader's explicit interface implementation of
          Dispose.

          //Tony

          "Jon Skeet [C# MVP]" <skeet@pobox.co mskrev i meddelandet
          news:17576f0f-6604-41ee-9df7-c8d288f91ec7@d1 g2000hsg.google groups.com...
          On Jun 17, 2:38 pm, "Tony" <johansson.ande rs...@telia.com wrote:
          >
          <snip>
          >
          So can you explain why Test in MyDerived class is called. It should be
          Test
          in MyBase according to my knowledge.
          >
          Well, you're reimplementing ITest (although note that you're doing it
          in a different way to my example - you're not using explicit interface
          implementation) .
          >
          Sections 13.4.5 and 13.4.6 of the C# 3 spec go into the details.
          >
          Jon

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Using interface and dispose

            On Jun 18, 7:22 am, "Tony" <johansson.ande rs...@telia.com wrote:
            Your example work the same even if I implement this Dispose implicit meaning
            that it's the Dispose in OddTextReader that is called.
            Yes - the results were the same (in this case), but I was just
            pointing out the difference in case you hadn't spotted it.
            The reason for this accoring to my knowledge is that
            the call to Dispose is beibng made as a being a Disposable.
            Yes.

            Jon

            Comment

            Working...