iterates a collection forward and reverse

   
 

using System;
using System.Collections.Generic;
public class Starter {
    public static void Main() {
        Console.WriteLine("Forward List");
        MyClass obj = new MyClass();
        foreach (int item in obj) {
            Console.Write(item);
        }

        Console.WriteLine("
Reverse List");
        foreach (int item in obj.Reverse) {
            Console.Write(item);
        }
    }
}

public class MyClass {

    private int[] list1 = new int[] { 0, 2, 4, 6, 8 };

    public IEnumerator<int> GetEnumerator() {
        for (int index = 0; index < 5; ++index) {
            yield return list1&#91;index&#93;;
        }
    }

    public IEnumerable<int> Reverse {
        get {
            for (int index = 4; index >= 0; --index) {
                yield return list1[index];
            }
        }

    }
}

    


Return IEnumerable by yield

   
 

using System;
using System.Collections;

public class Starter {
    public static void Main() {
        foreach (string day in ToEndOfMonth()) {
            Console.WriteLine(day);
        }
    }

    public static IEnumerable ToEndOfMonth() {
        DateTime date = DateTime.Now;

        int currMonth = date.Month;
        while (currMonth == date.Month) {
            string temp = currMonth.ToString() + "/" + date.Day.ToString();
            date = date.AddDays(1);
            yield return temp;
        }
    }
}

    


return IEnumerable

   
 


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);
        }
    }
}

    


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();
    }
}