Skip to content
Merged
8 changes: 4 additions & 4 deletions snippets/csharp/System/IDisposable/Overview/base1.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// <Snippet3>
using Microsoft.Win32.SafeHandles;
using System;
using System.IO;
using System.Runtime.InteropServices;

class BaseClass1 : IDisposable
{
// Flag: Has Dispose already been called?
bool disposed = false;
// Instantiate a SafeHandle instance.
SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
// Instantiate a FileStream instance.
FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);

// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
Expand All @@ -25,7 +25,7 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
handle.Dispose();
fs.Dispose();
// Free any other managed objects here.
//
}
Expand Down
8 changes: 4 additions & 4 deletions snippets/csharp/System/IDisposable/Overview/derived1.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// <Snippet4>
using Microsoft.Win32.SafeHandles;
using System;
using System.IO;
using System.Runtime.InteropServices;

class MyDerivedClass : MyBaseClass
{
// Flag: Has Dispose already been called?
bool disposed = false;
// Instantiate a SafeHandle instance.
SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
// Instantiate a FileStream instance.
FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);

// Protected implementation of Dispose pattern.
protected override void Dispose(bool disposing)
Expand All @@ -18,7 +18,7 @@ protected override void Dispose(bool disposing)

if (disposing)
{
handle.Dispose();
fs.Dispose();
// Free any other managed objects here.
//
}
Expand Down
10 changes: 5 additions & 5 deletions snippets/fsharp/System/IDisposable/Overview/base1.fs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module base1

// <Snippet3>
open Microsoft.Win32.SafeHandles
open System
open System.IO

type BaseClass1() =
// Flag: Has Dispose already been called?
let mutable disposed = false

// Instantiate a SafeHandle instance.
let handle = new SafeFileHandle(IntPtr.Zero, true)
// Instantiate a FileStream instance.
let fs = new FileStream("test.txt", FileMode.OpenOrCreate)

interface IDisposable with
// Public implementation of Dispose pattern callable by consumers.
Expand All @@ -22,8 +22,8 @@ type BaseClass1() =
override _.Dispose(disposing) =
if not disposed then
if disposing then
handle.Dispose()
fs.Dispose()
// Free any other managed objects here.
disposed <- true

// </Snippet3>
// </Snippet3>
9 changes: 5 additions & 4 deletions snippets/fsharp/System/IDisposable/Overview/derived1.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module derived1
open System
open System.IO

type MyBaseClass() =
// Flag: Has Dispose already been called?
Expand Down Expand Up @@ -30,18 +31,18 @@ type MyDerivedClass() =

// Flag: Has Dispose already been called?
let mutable disposed = false
// Instantiate a SafeHandle instance.
let handle = new SafeFileHandle(IntPtr.Zero, true)
// Instantiate a FileStream instance.
let fs = new FileStream("test.txt", FileMode.OpenOrCreate)

// Implementation of Dispose pattern.
override _.Dispose(disposing) =
if not disposed then
if disposing then
handle.Dispose()
fs.Dispose()
// Free any other managed objects here.

// Free any unmanaged objects here.
disposed <- true
// Call base class implementation.
base.Dispose disposing
// </Snippet4>
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
Option Strict On

' <Snippet3>
Imports Microsoft.Win32.SafeHandles
Imports System.IO
Imports System.Runtime.InteropServices

Class BaseClass : Implements IDisposable
Class BaseClass1 : Implements IDisposable
' Flag: Has Dispose already been called?
Dim disposed As Boolean = False
' Instantiate a SafeHandle instance.
Dim handle As SafeHandle = New SafeFileHandle(IntPtr.Zero, True)
' Instantiate a FileStream instance.
Dim fs As FileStream = New FileStream("test.txt", FileMode.OpenOrCreate)

' Public implementation of Dispose pattern callable by consumers.
Public Sub Dispose() _
Expand All @@ -23,7 +23,7 @@ Class BaseClass : Implements IDisposable
If disposed Then Return

If disposing Then
handle.Dispose()
fs.Dispose()
' Free any other managed objects here.
'
End If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Option Strict On
Imports System.IO
Imports System.Text.RegularExpressions

Public Class WordCount
Public Class WordCount2
Private filename As String
Private nWords As Integer
Private pattern As String = "\b\w+\b"
Expand Down Expand Up @@ -47,9 +47,9 @@ Public Class WordCount
End Class
' </Snippet2>

Public Module Example
Public Module Example2
Public Sub Main()
Dim wc As New WordCount("C:\users\ronpet\documents\Fr_Mike_Mass.txt")
Dim wc As New WordCount2("C:\users\ronpet\documents\Fr_Mike_Mass.txt")
Console.WriteLine("File {0} ({1}) has {2} words",
wc.Name, wc.FullName, wc.Count)
End Sub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
Option Strict On

' <Snippet4>
Imports Microsoft.Win32.SafeHandles
Imports System.IO
Imports System.Runtime.InteropServices

Class DerivedClass : Inherits BaseClass
Class DerivedClass2 : Inherits BaseClass2
' Flag: Has Dispose already been called?
Dim disposed As Boolean = False
' Instantiate a SafeHandle instance.
Dim handle As SafeHandle = New SafeFileHandle(IntPtr.Zero, True)
' Instantiate a FileStream instance.
Dim fs As FileStream = New FileStream("test.txt", FileMode.OpenOrCreate)

' Protected implementation of Dispose pattern.
Protected Overrides Sub Dispose(disposing As Boolean)
If disposed Then Return

If disposing Then
handle.Dispose()
fs.Dispose()
' Free any other managed objects here.
'
End If
Expand All @@ -31,7 +31,7 @@ Class DerivedClass : Inherits BaseClass
End Class
' </Snippet4>

Class BaseClass : Implements IDisposable
Class BaseClass2 : Implements IDisposable
' Flag: Has Dispose already been called?
Dim disposed As Boolean = False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Option Strict On

' <Snippet6>
Class DerivedClass : Inherits BaseClass
Class DerivedClass : Inherits BaseClass3
' Flag: Has Dispose already been called?
Dim disposed As Boolean = False

Expand All @@ -29,7 +29,7 @@ Class DerivedClass : Inherits BaseClass
End Class
' </Snippet6>

Class BaseClass : Implements IDisposable
Class BaseClass3 : Implements IDisposable
' Flag: Has Dispose already been called?
Dim disposed As Boolean = False

Expand Down