Classes

Classes are constructs enabling the creation of custom types through grouping variables of other types, methods, and events.

A class functions like a blueprint defining a type's behavior and data. If a class is not static, code can employ it through creating objects or instances assigned to variables. Those variables remain in memory until their references leave scope (garbage collection then manages it). If a class is static, code only accesses it with the class, and not with instance variables.

Declare classes with the class keyword. Review declaration syntax below:

public class Name
{
	//body
}

Review an example of a class below:

public class Snack
{
	public string chili;
	public string fruit;
	public string chocolate;
	public string cheese;
	public static void Main()
	{
		Snack chips = new Snack();
		chips.chili = “Chili Chips”;
		Console.WriteLine(“We stock”+chips.chili);
	}
}

OBJECT CREATION

Objects are not created by the class. Declarations of objects establish variables of the specified class resulting in a variable copy housing a reference to the object. The new keyword creates the object. Review object creation syntax below:

ClassName variableName = new ClassName();

Review an example of an object declaration:

Snack chips = new Snack();

Objects are accessed using the dot (.) operator. Review access syntax below:

object.member

Review an example of its use:

Snack chips = New Snack();
chips.chili = “Chili Chips”;

CONSTRUCTORS

When an instance or object of a class is created, the direct invocation of a constructor method occurs. This initializes the object and allocates memory. Every class contains at least one constructor, which carries the same name as its class.

C# offers two types of constructors: static and instance. Static constructors initialize static variables of a class, and utilize the static keyword. They store shared values and offer implicit private access. They are presented first within the class. Instance constructors initialize member variables of a class, and do not have return types, but can have access modifiers.

Instance constructors include the following types: default constructors, initializes a variable with a value; parameterized constructors, constructors with at least one parameter; copy constructors, constructors with parameters of the same class type; and private constructors. Review an example of constructors below:

class Pizza
{
	string ON, GP, OL, SSG, toppings;
	Pizza()
	{
		ON = “onions”;
		GP = “green peppers”;
		OL = “olives”;
		SSG = “sausage”;
	}
	public void AddTopping()
	{
		toppings = ON+”and”+GP;
	}
	public void ShowOrder()
	{
		Console.WriteLine(“Your pizza toppings
are{0}”+toppings);
	}
	public static void Main()
	{
		Pizza newOrder = new Pizza();
		newOrder.AddTopping();
		newOrder.ShowOrder();
	}
}

DESTRUCTORS

Destructors release instances of a class from memory, which optimizes application performance. Cleanup, despite C# garbage collection, is also a best practice. A prefix of the “~” character in a method name will destroy a method of the same name:

//Constructor
Pizza()
{
	Console.WriteLine(“Construction.”);
}
//Deconstructor
~Pizza()
{
	Console.WriteLine(“Destruction.”);
}

LIFE CYCLE

The general life cycle of an object follows:

  1. Memory is allocated by the new operator.
  2. The object constructor is called.
  3. The application uses the object.
  4. When references fall out of scope or are set to null, the object is destroyed.
  5. The object's memory becomes available to other objects.