Is this example a bad usage of Idispose?

时间:2015-12-14 17:57:52

标签: vb.net

I was wondering if my usage of Idispose in the situation below was discouraged since I do not use it to free any resources, but only to reset a temporary change.

So, according to you, is it bad design ? And if yes, what are the reasons behind your answer ? (performance cost, absolute ugliness, etc...)

I just found a class that was using something similar and I was liking the idea but also wondering if that was inherently bad or not.

Example

Using pos As New ConsoleCursorScope(0, 10)
    Console.Write("This is the first line.")
End Using
    Console.WriteLine("This is the second line.")

The class

   Public Class ConsoleCursorScope
    Implements IDisposable


    Public Property Left() As Integer
    Public Property Top() As Integer


    Public Sub New(Left As Integer, Top As Integer)
        _Left = Console.CursorLeft
        _Top = Console.CursorTop
        Console.SetCursorPosition(Left, Top)
    End Sub

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not disposedValue Then
            If disposing Then
                Console.CursorLeft = _Left
                Console.CursorTop = _Top
            End If
        End If
        disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
    End Sub
#End Region
End Class

0 个答案:

没有答案