locked
You are creating a generic class, and you need to dispose of the generic objects. How can you do this? RRS feed

  • Question

  • Hi,

    I've found this question in a 70-535 (.net framework development foundation test) training book. The correct answer is D), but I don't know why, or how could I use these constrains...

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


    Thanks
    Blog de desarrollo / Development blog
    Monday, March 30, 2009 9:44 AM

Answers

  • Hi,

    you can restrict the Generic type

    So Public Class foo (Of T As IDisposable)
    Or
    public  class foo<T> where T : IDispoable

    This means you can have any type as long as it implements IDisposable.

    Hope this helps and I have the syntax right (from memory)

    Geoff

    MCPD * 5


    • Proposed as answer by Geoff Clark MCM Monday, March 30, 2009 10:37 AM
    • Marked as answer by Jesús Bosch Monday, March 30, 2009 10:45 AM
    Monday, March 30, 2009 10:36 AM

All replies

  • Hi,

    you can restrict the Generic type

    So Public Class foo (Of T As IDisposable)
    Or
    public  class foo<T> where T : IDispoable

    This means you can have any type as long as it implements IDisposable.

    Hope this helps and I have the syntax right (from memory)

    Geoff

    MCPD * 5


    • Proposed as answer by Geoff Clark MCM Monday, March 30, 2009 10:37 AM
    • Marked as answer by Jesús Bosch Monday, March 30, 2009 10:45 AM
    Monday, March 30, 2009 10:36 AM
  • Ozzy's right - using the constraint means that whatever type is substituted for generic type "T" at compile time will have to implement IDisposable.  But the question is a little confusing because imposing the constraint by itself doesn't actually dispose of anything. 

    I agree - this question is a little unclear.  It might be better if they asked: "you need to ensure that the generic object can be disposed of", or something like that.

    HTH

    Monday, March 30, 2009 2:55 PM
  • I think all certification questions are so confusing ;)


    Blog de desarrollo / Development blog
    Monday, March 30, 2009 8:52 PM