Summary: in this tutorial, you’ll learn about the C# class and how to define a custom class.
Introduction to the objects and classes
Objects are one of the essential concepts in object-oriented programming. Objects have states and behaviors:
- States represent the data that the object holds at a particular point in time.
- Behaviors represent the actions that the object can perform to manipulate its states.
C# uses class-based object-oriented programming. Before creating objects, you need to define a class. A class is a blueprint for creating objects.
Define a class
To define a new class, you use the class keyword followed by the class name. By convention, a class name is in the Pascal case, such as Person, SalesPerson, etc.
To create a new class, you follow these steps:
First, create a new Person.cs file.
Second, define the Person class in the Person.cs file:
class Person
{
}Code language: C# (cs)Create objects from the class
To create objects from the Person class, you follow these steps:
First, create the Program.cs file.
Second, create a new object from the Person class by using the new keyword:
Person p1 = new Person();Code language: C# (cs)Or you can use the var keyword:
var p1 = new Person();Code language: C# (cs)Or
Person p1 = new();Code language: C# (cs)In this example, the p1 is a new object of the Person class. It is also called an instance of the Person class.
Add fields to the class
The following add three fields to the Person class: FirstName, LastName, and Age. These fields represent the states of a person object:
class Person
{
public string FirstName;
public string LastName;
public byte Age;
}Code language: C# (cs)The public keyword controls the access level to access the fields from both inside and outside the Person class. See the public & private access modifier tutorial for more information.
Since the Person class has three fields, and all of its objects can access them. For example, the following creates a new instance of the Person class and assigns the values to each field:
using HR;
Person p1 = new();
p1.FirstName = "John";
p1.LastName = "Doe";
p1.Age = 25;Code language: C# (cs)From the Person class, you can create as many objects as you want to. For example, the following creates two Person’s objects with the names p1 and p2:
using HR;
Person p1 = new();
p1.FirstName = "John";
p1.LastName = "Doe";
p1.Age = 25;
Person p2 = new();
p2.FirstName = "Jane";
p2.LastName = "Doe";
p2.Age = 22;Code language: C# (cs)Note that p1 and p2 have the same set of fields. However, their field values are different.
Add methods to a class
The following adds the GetFullName() method to the Person class:
// Person.cs
class Person
{
public string FirstName;
public string LastName;
public byte Age;
public string GetFullName()
{
return $"{FirstName} {LastName}";
}
}Code language: C# (cs)The GetFullname() is like a function with the public keyword. When you define a function inside a class, it is called a method.
The GetFullName() method concatenates the first name with the last name and returns the full name as a string.
The public keyword indicates that it is accessible from both inside and outside the Person class.
To call the GetFullName() method from a Person object, you use the object name, dot operator, and the method name. For example:
// Program.cs
Person p1 = new();
p1.FirstName = "John";
p1.LastName = "Doe";
p1.Age = 25;
Console.WriteLine(p1.GetFullName());
Person p2 = new();
p2.FirstName = "Jane";
p2.LastName = "Doe";
p2.Age = 22;
Console.WriteLine(p2.GetFullName());Code language: C# (cs)Output:
John Doe
Jane DoeCode language: C# (cs)Summary
- Objects have states and behaviors.
- C# uses a class-based object-oriented programming approach.
- A class is a blueprint for creating objects. Objects are instances of a class.
- Use the
classkeyword to define a class. - A class has fields and methods.
- Use the
newkeyword to create a new object from a class.