Understanding Object Lifetime
.NET objects are allocated in a memory called managed heap, where they will be automatically destroyed by the garbage collector.
Before going into details you have to know about the class, object, reference, stack and heap.
A class is nothing but a blueprint that describes how an instance of this type will look and feel in memory. Classes, of course, are defined within a code file (which in C# takes a *.cs extension).
Consider a simple Car class defined in a C# Console Application project named SimpleGC:
//Car.cs
public class Car
{
private int currSp;
private string petName;
public Car() { }
public Car(string name, int speed)
{
petName = name;
currSp = speed;
}
public override string ToString()
{
return string.Format("{0} is going {1} MPH",
petName, currSp);
}
}
Once a class is defined, you can allocate any number of objects using the C# new keyword.
Understand, that the new keyword returns a reference to the