C# is a popular multi-paradigm programming language, which uses the object-oriented paradigm. In this article, you will learn how to create and use classes in C#.

Creating a Class Declaration

In C#, a class is a reference type, which will contain the null value until you create a new object of the class. To create a new class in C# you will need several components:

An access modifier. The class keyword. The name you want to assign to the class. A pair of open and close curly braces (which will enclose the attributes, constructors, and methods of the class).

The code above creates a new class that is accessible by other classes in the same assembly (compilation file). C# has exactly six access modifiers that you can use to control the accessibility levels of your classes, variables, and methods. The six access modifiers are:

public: every class (regardless of its assembly) can access the public class and its public members (attributes, constructors, and methods). private: only the private class and its members can access it. protected: only derived classes (child and grandchildren classes) can access the protected class. internal: only classes in the same assembly can access the internal class. protected internal: only classes in the same assembly or a derived class from another assembly can access the protected internal class. private protected: only derived classes in the same assembly can access the private protected class.

Declaring and Accessing Attributes

Attributes are the building blocks for the classes you create. They hold sensitive data and usually have a private or protected access modifier. Therefore, to access these classes from external classes you would need to use accessors and mutators (getters and setters).

C# allows you to declare your attributes, mutators, and accessors as follows:

Other popular object-oriented languages also use the approach above. In fact, if you want to create Java classes, you will have to use the structure above. However, C# now has an easier way of creating attributes and accessors. In C#, this class has the same function as the one above:

The class above contains what C# calls properties, which is a combination of attributes(fields) and methods. With properties, you can reduce your attribute declaration, mutators, and accessors code by half.

Declaring Constructors

Constructors are another fundamental aspect of a class. To create an object from a class you will have to call one of its constructors. Each constructor has an optional access modifier and the same name as its class. For object-oriented programming languages, there are generally three types of constructors:

Default constructor: takes no arguments and provides each attribute with a default value. Primary constructor: takes one or more arguments (depending on the number of variables in the class). Copy constructor: takes another constructor as an argument.

C# has an umbrella term for the default and primary constructors above—instance constructors. This programming language also has two other constructors (private and static). This article focuses on the three traditional constructors.

Default Constructor

Primary Constructor

Copy Constructor

Creating Methods

Methods are not a crucial class component, but they are useful. A class can have one or more methods. A method has an access modifier, a return type, a name, and a body.

The code above returns a string representation of the customer object.

Creating Objects

After creating a complete class, equipping it with attributes, constructors, and a method, you can start creating objects using the different constructors. To create an object with no attributes, you can use the default constructor:

The line of code above creates a default customer and assigns it to a variable called John. With John, you can access the default value of each customer attribute.

Executing the code above prints the following in the console:

You can also use the John variable to access any method in the customer class.

Executing the line of code above prints the following output in the console:

To create an object with attributes, you would use the primary constructor:

Executing the code above prints the following output in the console:

To create a copy of the object above you can use the copy constructor:

Executing the code above prints the following output in the console:

As you can see the copy constructor is a copy of the primary constructor. The copy constructor can also take a default constructor as an argument:

Executing the code above prints the following output in the console:

Now You Can Create and Use C# Classes

You can use the object-oriented paradigm in C# to define classes and create objects from them. You can create methods for each class which can then operate on their objects’ attributes.

However, the object-oriented paradigm is not the only one you need to be familiar with. The top three programming paradigms are imperative, object-oriented, and functional.