about Interface IDisposable

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

    about Interface IDisposable

    Hello!

    I have the following question:
    You are creating a generic class, and you need to dispose of the generic
    objects.
    How can you do this?

    A. Call the Object.Dispose method.
    B. Implement the IDisposable interface.
    C. Derive the generic class from the IDisposable class.
    D. Use constraints to require the generic type to implement the IDisposable
    interface.

    The right answer is D but I don't fully understand why this is the right
    answer.

    Assume I create a generic class called Farm. This Farm can contain Animals
    like this.
    public class Farm<Animals>
    {
    ....
    }

    Now to my question if this generic class Farm<Animalsimp lement
    IDisposable
    then method Dispose can be called to dispose of the generic objects.

    I mean if I want to dispose all of the Animals object I can just call the
    dispose method located in
    the farm class but if I call the dispose method in the Animal class
    the Farm object is still there.


    Can anybody make a comment about my discussion.


    //Tony



  • Peter Morris

    #2
    Re: about Interface IDisposable

    You must remember the purpose of IDisposable. If for example you have a
    class that uses Windows API calls to create a handle then you would
    implement IDisposable to ensure that you class releases that resource when
    it is finished with. The Dispose method will be executed either via the
    IDisposable method or from your class finalizer, but either way it will be
    executed and will release the resource. This is an IDisposable
    implementation with a finalizer!

    You could additionally implement IDisposable on your Farm class which just
    calls Dispose() on each of the animals held within it, in this case the Farm
    would not need a finalizer because you don't need to absolutely guarantee it
    is called on Farm, only on Animal (which has already been covered above).
    In this case the Dispose on Farm is just a convenience.



    --
    Pete
    ====



    Comment

    • Tony Johansson

      #3
      Re: about Interface IDisposable

      Hello!

      So why is alternative D better then B ?

      //Tony


      "Peter Morris" <mrpmorrisNO@SP AMgmail.comskre v i meddelandet
      news:eLBS0nvKJH A.4280@TK2MSFTN GP04.phx.gbl...
      You must remember the purpose of IDisposable. If for example you have a
      class that uses Windows API calls to create a handle then you would
      implement IDisposable to ensure that you class releases that resource when
      it is finished with. The Dispose method will be executed either via the
      IDisposable method or from your class finalizer, but either way it will be
      executed and will release the resource. This is an IDisposable
      implementation with a finalizer!
      >
      You could additionally implement IDisposable on your Farm class which just
      calls Dispose() on each of the animals held within it, in this case the
      Farm would not need a finalizer because you don't need to absolutely
      guarantee it is called on Farm, only on Animal (which has already been
      covered above). In this case the Dispose on Farm is just a convenience.
      >
      >
      >
      --
      Pete
      ====

      http://www.capableobjects.com

      Comment

      • Peter Morris

        #4
        Re: about Interface IDisposable

        "You are creating a generic class, and you need to dispose of the generic
        objects."

        It's a bit of a vague question really because it doesn't say how the class
        passed will be used

        public class ObjectAndAHandl e<T>
        {
        public T Value { get; set; }
        IntPtr Handle { get; }
        }

        You might need to dispose of THIS object *if* it had an unmanaged resource
        like a file handle or something, but would have no need to call Dispose on T
        unless it happens to implement it.


        public class SomeKindOfList< T>
        {
        public readonly List<TList { get; }
        }

        No need to dispose of the list, but you *might* want to dispose of the
        objects inside, however you cannot possibly know because it is generic!


        If the question was something like:
        "You want to implement a generic class list which disposes of the objects it
        references when it is disposed" then the answer would be both B and D.


        In summary, the question is crap :-)



        --
        Pete
        ====







        Comment

        Working...