C# Class

In this section, we'll demonstrate the concepts of a class and an object in C#, the overview of access modifiers and a simple example.

  • A class is a blueprint containing all types of data and relevant behaviours.
  • A class is an user defined type so variables can be declared as this type as usual.
  • If a class is not static, an object or an instance of the class can be created and assigned to a variable.
  • A variable of a class is a reference-typed so the value of the variable holds the address of an object if an object is assigned.
  • A class defines the type of an object and an object is a concrete entity realizing the definition of a class.
  • In a program, a class can be defined once and many objects of the class can be created.

We use "class" keyword to declare a class, the syntax is shown below.

<Access Modifier> class <class name>
{
	<Access Modifier> <data type> variable1;
	<Access Modifier> <data type> variable2;
	...

	<Access Modifier> <return data type> method1([<data type> parameter1, <data type> parameter1, ...]);
	<Access Modifier> <return data type> method1([<data type> parameter1, <data type> parameter1, ...]);
	...
}
  • An object is the realization of a class in memory and includes each item defined in the class.
  • Many objects of the same class can be created in a program.
  • An object is also called an instance which is instantiated from a class.
  • The keyword "new" is used to create an object of a class.

The syntax to create an object is like below.

<Class Name> variable = new <Class Name>();

Example 01-41-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;

public class Computer
{
	private int yearbuilt;
	public string brand;

	public int getYearBuilt()
	{
		return yearbuilt;
	}

	public void setYearBuilt(int yearbuilt)
	{
		this.yearbuilt = yearbuilt;
	}

	public static void Main()
	{
		Computer mypc = new Computer();
		mypc.yearbuilt = 2008;
		mypc.brand = "Dell";
		
		Console.WriteLine("The computer was built from {0} in {1}.", mypc.brand, mypc.yearbuilt);
	}

}

Output

The computer was built from Dell in 2008.

Explanation

  • Line 3: Define a class named "Computer". A class name always starts with a capital character.
  • Line 5: Declare a private int variable "yearbuilt". The variable is also called a field of the class.
  • Line 6: Declare a public string field "brand".
  • Line 8-11: Define a method "getYearBuilt". The method has no parameter and returns an int value.
  • Line 10: return the value of the field "yearbuilt".
  • Line 13-16: Define another method "setYearBuilt". The method has a parameter "yearbuilt" which is different than the class field "yearbuilt".
  • Line 15: "this" means current instance or object. So this statement will assign the parameter's value to the field "yearbuilt".
  • Line 18-25: Main method. It is the start entry to run in the program.
  • Line 20: Create an object named "mypc" of the class Computer.
  • Line 21: Assign 2008 to the field "yearbuilt" of the object "mypc".
  • Line 22: Assign Dell to the field "brand" of the object "mypc".
  • Line 24: Output the string including the values of the fields.

Access modifiers are used to define the accessibility levels of all types and type members including classes, fields and methods, etc. The table below is listing all the access modifiers in C#, their descriptions and the examples.

Access ModifiersDescriptionsExamples
publicIt can be accessed by any code in any assemblies which have referenced it. public bool isFound;
privateIt can be accessed only by the code in the same class or struct. private Computer mypc;
protectedIt can be accessed only by the code in the same class, derived class or struct. protected int age = 18;
internalIt can be accessed only by the code in the same assembly. internal float ff = 3.14F;
protected internalIt is protected and internal. protected internal string ss;

The default access modifier of a class is internal. It means the class is in internal access level if no access modifier is specified before the class keyword. For each access modifier, you can click the above link to go to the detailed explanation page.