using System;
using System.Collections.Generic;
class IteratorDemo {
static IEnumerable<int> OneTwoThree() {
Console.WriteLine("Returning 1");
yield return 1;
Console.WriteLine("Returning 2");
yield return 2;
Console.WriteLine("Returning 3");
yield return 3;
}
static void Main() {
foreach (var number in OneTwoThree()) {
Console.WriteLine(number);
}
}
}
Month: February 2011
yield return
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
class Program {
public static IEnumerable<string> SimpleList() {
yield return "1";
yield return "2";
yield return "3";
}
public static void Main(string[] args) {
foreach (String item in SimpleList())
Console.WriteLine(item);
}
}
The Dispose Pattern
using System;
public class MyClass {
private string name;
public MyClass(string name) { this.name = name; }
override public string ToString() { return name; }
~MyClass() { Console.WriteLine("~MyClass()"); }
public void Dispose() {
Console.WriteLine("Dispose()");
}
}
public class GarbageDisposalApp {
public static void Main(string[] args) {
DoSomething();
Console.WriteLine("end of Main");
}
public static void DoSomething() {
MyClass t = new MyClass("Foo");
Console.WriteLine(t);
t.Dispose();
t = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
using statement with IDisposable interface
using System;
public class MyValueReport {
int InstanceNumber;
public MyValueReport(int InstanceNumber) {
this.InstanceNumber = InstanceNumber;
Console.WriteLine(InstanceNumber);
}
}
public class MyValue : IDisposable {
int n;
public MyValue(int n) {
this.n = n;
MyValueReport MyReport = new MyValueReport(n);
}
public void Dispose() {
MyValueReport MyReport = new MyValueReport(this.n);
GC.SuppressFinalize(this);
}
~MyValue() {
MyValueReport MyReport = new MyValueReport(this.n);
}
}
public class Test {
static void Main() {
MyValue d1 = new MyValue(1);
MyValue d2 = new MyValue(2);
MyValue d3 = new MyValue(3);
d1 = null;
using (d3) {
MyValue d4 = new MyValue(4);
}
d2.Dispose();
}
}
The constructor initializes the internal object. The Dispose method closes the file resource. The destructor delegates to the Dispose method.
using System;
using System.IO;
public class WriteToFile : IDisposable {
public WriteToFile(string _file, string _text) {
file = new StreamWriter(_file, true);
text = _text;
}
public void WriteText() {
file.WriteLine(text);
}
public void Dispose() {
file.Close();
}
~WriteToFile() {
Dispose();
}
private StreamWriter file;
private string text;
}
public class Writer {
public static void Main() {
WriteToFile sample = new WriteToFile("sample.txt", "My text file");
sample.WriteText();
sample.Dispose();
}
}
Protecting Against Double Disposal
using System;
public class MyClass : IDisposable
{
private string name;
public MyClass(string name) { this.name = name; }
override public string ToString() { return name; }
~MyClass()
{
Dispose();
Console.WriteLine("~MyClass()");
}
private bool AlreadyDisposed = false;
public void Dispose()
{
if (!AlreadyDisposed)
{
AlreadyDisposed = true;
Console.WriteLine("Dispose()");
GC.SuppressFinalize(this);
}
}
}
public class MainClass
{
public static void Main(string[] args)
{
MyClass t = new MyClass("Foo");
Console.WriteLine(t);
t.Dispose();
t.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
Derived Disposable Classes
using System;
public class MyClass : IDisposable
{
protected string name;
public MyClass(string name) { this.name = name; }
override public string ToString() { return name; }
~MyClass() { Dispose(); Console.WriteLine("~MyClass()"); }
public void Dispose()
{
Console.WriteLine("MyClass.Dispose()");
GC.SuppressFinalize(this);
}
}
public class SonOfMyClass : MyClass, IDisposable
{
public SonOfMyClass(string name) : base(name) { }
override public string ToString() {
return name;
}
~SonOfMyClass() {
Dispose();
Console.WriteLine("~SonOfMyClass()");
}
new public void Dispose()
{
base.Dispose();
GC.SuppressFinalize(this);
}
}
class DerivedDisposeApp
{
static void Main(string[] args)
{
DoSomething();
}
static void DoSomething()
{
SonOfMyClass s = new SonOfMyClass("Bar");
Console.WriteLine(s);
s.Dispose();
}
}

