Computer >> Computer tutorials >  >> Programming >> C#

Explain the concept of a class in C#


A class is one of the essential types in C#. We can think of a class as a blueprint for the objects relevant to the problem domain. It's a template from which we create the objects, defining the structure and behavior that will be shared by the set of objects created from this class. In simple words, a class is a cookie-cutter, and the objects are the cookies themselves.

A class also enables encapsulation, which is an important concept in object-oriented programming. It means combining the data and the operations that work on the data in a single place and providing a simple API to the users of that object. A class allows us to encapsulate the data and hides the irrelevant details from other classes.

We can create a class by using the class keyword, followed by the name of the class.

// User.cs
public class User{
   private string name;
   private int salary;
   public void Promote(){
      salary += 1000;
   }
}

In the above example, User is a class that represents the users. The class encapsulates two pieces of data − name and salary. These are known as class fields, and hold the users' name and salary. It also has a method named Promote(), which raises the user's salary.

Each class has an associated access modifier that controls whether the class will be visible to other classes. Here are the five possible values we can provide for the access modifier.

Access ModifierDescription
publicUnlimited access
protectedLimited access to the derived classes
internalLimited access to the assembly
protected internalLimited access to the assembly or the derived classes
privateNo outside access

To create an instance of a class, we can use the new keyword. The new operator calculates the number of bytes required by the object's data and allocates memory for the object. Then it returns a pointer (also called a reference) to the newly created object.

var alice = new User();
var bob = new User();

This reference is then stored in the variable that's on the left side of the = sign. In the above example, Alice and Bob hold the references or pointers to the newly created objects.

Explain the concept of a class in C#

In C#, the naming convention for a class follows the PascalCase which capitalizes the first letter of each word in a compound word, e.g. StringBuilder, UserController, etc. It's not necessary to create a class in a file whose name matches the class name. However, it's a convention used by most of the C# projects.

Constructors

In the above example, when we created the instances of the User class, i.e. alice and bob, we didn't provide their initial names and salaries. Often a newly created object needs some information to do its job, and a constructor is used to initialize the class data.

We can add a constructor to give a user its name and salary as follows −

public class User{
   private string name;
   private int salary;
   public User(string name, int salary){
      this.name = name;
      this.salary = salary;
   }
   public void Promote(){
      salary += 1000;
   }
}

Having a constructor allows us to pass the name and the salary of a user when creating a new instance.

var alice = new User("Alice", 50000);
var bob = new User("Bob", 45000);

It's possible to have more than one constructor in a class. Having multiple constructors allows us to initialize the class in different ways. For example, we could add another constructor that only takes the user's name and assigns a default salary.

public User(string name){
   this.name = name;
   this.salary = 50000;
}

Example

using System;
class Program{
   static void Main(){
      var alice = new User();
      alice.Print();

      var bob = new User();
      bob.Print();
      var chris = new User("Chris", 50000);
      chris.Print();
      var debs = new User("Debs", 45000);
      debs.Print();

      var scott = new User("Scott");
      scott.Print();
   }
}
public class User{
   private string name;
   private int salary;
   public User(){

   }
   public User(string name){
      this.name = name;
      this.salary = 50000;
   }
   public User(string name, int salary){
      this.name = name;
      this.salary = salary;
   }
   public void Promote(){
      salary += 1000;
   }
   public void Print(){
      Console.WriteLine($"{name}: {salary}");
   }
}

Output

: 0
: 0
Chris: 50000
Debs: 45000
Scott: 50000