Jako że dopiero zaczynam przygodę z C# prosiłbym o przetłumaczenię tych programów co klasy i pozostałe elementy robią w tych programach. Jestem póki co zielony w tych sprawach a to mi ułatwi przyszłe działania.

Program 1 próbowałem coś zadziałać:
- zmienna typu boolean
- pusta metoda Dispose()
- if ( jeśli nie wykonało alredy...) przypisz true do alredy disposed
- Klasa Main utowrzenie obiektu i referencji (t) i parametr foo,wypis na konsole


Program 1:
Kod:
1 Program:
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();
}
}

Program 2:
Kod:
using System;

public class Name {
public string firstName;
public string lastName;

public Name(string firstName, string lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public void Display() {
Console.WriteLine("firstName = " + firstName);
Console.WriteLine("lastName = " + lastName);
}

// override the ToString() method
public override string ToString() {
return firstName + " " + lastName;
}
}

class Test{
public static void Main() {
Name myName = new Name("T", "M");
Name myOtherName = new Name("P", "B");

// call the ToString() method for the Name objects
Console.WriteLine("myName.ToString() = " + myName.ToString());
Console.WriteLine("myOtherName.ToString() = " + myOtherName.ToString());
}
}
Program 3:
Kod:
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example7_7.cs illustrates how to override the ToString() method
*/

using System;


// declare the Car class
class Car
{

// declare the fields
public string make;
public string model;

// define a constructor
public Car(string make, string model)
{
this.make = make;
this.model = model;
}

// override the ToString() method
public override string ToString()
{
return make + " " + model;
}

}


public class Example7_7
{

public static void Main()
{

// create Car objects
Console.WriteLine("Creating Car objects");
Car myCar = new Car("Toyota", "MR2");
Car myOtherCar = new Car("Porsche", "Boxter");

// call the ToString() method for the Car objects
Console.WriteLine("myCar.ToString() = " +
myCar.ToString());
Console.WriteLine("myOtherCar.ToString() = " +
myOtherCar.ToString());

}

}
Program 4:
Kod:
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(): " +name);
}

public void Dispose()
{
Console.WriteLine("Dispose(): " +name);
GC.SuppressFinalize(this);
}
}


public class DisposableApp
{
public static void Main(string[] args)
{
Console.WriteLine("start of Main, heap used: {0}", GC.GetTotalMemory(true));
DoSomething();
Console.WriteLine("end of Main, heap used: {0}", GC.GetTotalMemory(true));
}

public static void DoSomething()
{
MyClass[] ta = new MyClass[3];

for (int i = 0; i < 3; i++)
{
ta[i] = new MyClass(String.Format("object #" +i));
Console.WriteLine("Allocated {0} objects, heap used: {1}", i+1, GC.GetTotalMemory(true));
}

for (int i = 0; i < 3; i++)
{
ta[i].Dispose();
ta[i] = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Disposed {0} objects, heap used: {1}",i+1, GC.GetTotalMemory(true));
}
}
}

Program 5:
Kod:
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();
}
}