Start learning free

LESSON 281 OF 674 · FUNCTIONS & CLASSES

Classes in C#

Lesson

C# is an object oriented programming language. Almost everything in C# is an object, with its attributes and methods.

Class creates a user-defined data structure, which holds its own data members (attributes) and member functions (methods), which can be accessed and used by creating an instance of that class.

Classes are like a blueprint for objects outlining possible behaviors and states that every object of a certain type could have.

Classes are defined via the keyword class, like this:

// Define an empty class Employee
class Employee {

}

Objects/Instances

An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. It’s not an idea anymore, it’s an actual employee, like a person named "Andrew" who is 30 years old.

You can have many employees to create many different instances, but without the class as a guide, you would be lost, not knowing what information is required.

After a class is defined, we can create objects of the class using the new keyword like this:

// Define an empty class Employee
class Employee {

}

// inside a method, for example Main:
Employee employee = new Employee();

Attributes/Properties

A class attribute (a field) is a C# variable that belongs to a class; every object of this class has its own copy of it. Let's add some attributes to our Employee class:

class Employee {
	public string name;
	public int age;
}

// inside a method, for example Main:
Employee emp1 = new Employee();
	
emp1.name = "Andrew";
Console.WriteLine(emp1.name); // prints Andrew
	
Employee emp2 = new Employee();
	
emp2.age = 40;
Console.WriteLine(emp2.age); // prints 40

We added two properties: the string name and the int age. Then, we created two different Employee objects emp1 and emp2.

The attributes of objects can be accessed and modified using the dot operator (.), like we did above with emp1.name and emp2.age.

The keyword public used inside the class is called an access specifier. name and age are public attributes, which means they can be accessed from anywhere in the code, not just inside the class code.

Constructors

In real life, we want to set the attributes specific to each object when creating it. For example, an employee named Andrew of 30 years old and another one named Mary of 25 years old. Here the constructor comes to rescue.

A constructor is a special type of method (function) which is used to initialize the instance members of the class.

In C# the constructor method has the same name as that of its class and is always called when an object is created.

Let's add a constructor to our Employee class:

class Employee {
	public string name;
	public int age;
	
	// constructor:
	public Employee(string name, int age) {
		this.name = name;
		this.age = age;
	}
}

// inside a method, for example Main:
Employee emp1 = new Employee("Andrew", 30);
	
Console.WriteLine(emp1.name);
	
Employee emp2 = new Employee("Mary", 25);

Console.WriteLine(emp2.age);

// prints Andrew and 25 on different lines

The "this"

Every object in C# has access to itself through a reference called this.

The this reference is available inside all instance methods. Therefore, inside a member function, this may be used to access and manipulate the attributes of that object, like we did above with this.name = name.

Member functions

Member functions are functions that belong to the class:

class Employee {
	public string name;
	public int age;
	
	// constructor:
	public Employee(string name, int age) {
		this.name = name;
		this.age = age;
	}
	
	//member functions:
	public void PrintName() {
		Console.WriteLine(name); // it is not mandatory to use "this" to access an attribute
	}
		
	public void PrintEmployee() {
		PrintName();
		Console.WriteLine(age);
	}
}

// inside a method, for example Main:
Employee emp1 = new Employee("Andrew", 30);

emp1.PrintEmployee(); // prints "Andrew", 30
	
Employee emp2 = new Employee("Mary", 25);

emp2.PrintEmployee(); // prints "Mary", 25

As you can see, we access functions just like we access attributes: using the dot syntax (.).

The function call uses the attributes of the object it was called on. That's why the first PrintEmployee() prints Andrew and 30 and the second prints Mary and 25.


Assignment
Follow the Coding Tutorial and let's write some classes.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 8
    Ctrl + Enter to check