15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

esource Management ❘ OC179<br />

public:<br />

virtual void Foo() override<br />

{<br />

Base::Foo();<br />

}<br />

virtual void Bar() override<br />

{<br />

}<br />

};<br />

code snippet CPPCLI/InheritanceDemo.h<br />

' Visual Basic<br />

Public MustInherit Class Base<br />

Public Overridable Sub Foo()<br />

End Sub<br />

Public MustOverride Sub Bar()<br />

End Class<br />

Public class Derived<br />

Inherits Base<br />

Public Overrides Sub Foo()<br />

MyBase.Foo()<br />

End Sub<br />

Public Overrides Sub Bar()<br />

End Sub<br />

End Class<br />

code snippet VisualBasic/InheritanceDemo.vb<br />

// F#<br />

[]<br />

type Base() as this =<br />

abstract Foo : unit -> unit<br />

default this.Foo() = printfn "Base.Foo"<br />

abstract Bar : unit -> unit<br />

type Derived() as this =<br />

inherit Base()<br />

override this.Foo() =<br />

base.Foo()<br />

printfn "Derived.Foo"<br />

override this.Bar() = printfn "Derived.Bar"<br />

code snippet FSharp/InheritanceDemo.fs<br />

tesourCe managemenT<br />

Working with resources is covered in Chapter 13, “Memory Management <strong>and</strong> Pointers,” both implementing<br />

the IDisposable interface <strong>and</strong> implementing a finalizer. How this looks in C++/CLI, Visual Basic, <strong>and</strong> F#<br />

is covered in this section.<br />

idisposable interface implementation<br />

For freeing resources, the interface IDisposable defines the Dispose() method. Using <strong>C#</strong>, Visual Basic,<br />

<strong>and</strong> F# you have to implement the interface IDisposable. With C++/CLI the interface IDisposable is<br />

implemented as well, but this is done by the compiler if you just write a destructor:<br />

// <strong>C#</strong><br />

public class Resource: IDisposable<br />

{<br />

public void Dispose()<br />

{<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!