C# class

In the previous sections, you have seen that how we can create simple datatypes like int, string, float etc in the C#. But what you will do if there is a need of custom datatype?

To create custom datatypes we can make use of C# class. Basically, C# class is a combination of data and behavior. The fields of a C# class often termed as class data and class methods as behavior.

Let's understand the different parts of a C# class using an example.

public class Student
{
    string _firstName = string.Empty;  // Class field also called -> state or data of class
    string _lastName = string.Empty;    // Class field also called -> state or data of class
    private string _fullName = string.Empty;  // Class field also called -> state or data
    int _age = 0;
 
    public string FirstName { get; set; } // Auto implemented FirstName property
    public string LastName { get; set; } // Auto implemented LastName property
 
    public string FullName      //Property of a class, internally this is same as auto implemented property
    { 
        get
        {
            return _fullName;                
        }
        set
        {
            _fullName = value;
        }
    }
 
    public Student() //Constructor of Student class, Constructor name must be same as class name.
    {
 
    }
 
    public Student(string firstName, string lastName) //Parameterized Constructor of Student class, Constructor name must be same as class name.
    {
        _firstName = firstName;
        _lastName = lastName;
    }
 
    public void PrintStudentDetail(string firstName, string lastName)  //Method or function, also called behavior of a class
    {
        Console.WriteLine("Student first name is : {0} and student last name is : {1}", firstName, lastName);
    }
 
    ~Student() //Destructor of a class automatically called by garbage collector. Destructor name must be same as class name. 
    {
        
    }
}

Field Name

The field is a C# class variable which can be of any valid data type. In the above example, _firstName, _lastName, _fullName, _age are the fields of Student class. A class may have instance fields or static fields or both. The class fields are also named as state or data of a class.

Properties

Properties are a way to encapsulate and protect the class fields. To implement the properties get and set accessors are used. If get and set both accessors are being used in a property then that property is called as Read/Write property. A property becomes Read Only if only get accessor used inside it, similarly, it becomes Write Only if only on set accessor used.

Example :

public string FullName { get; set; } //Read/Write property
public string FirstName { get;  } //Read Only property
public string LastName { set; } //Write Only property

Constructor

The constructor must have the same name as its Class name, Constructor is a way of initializing the class fields. A Constructor can have parameters but, it never returns a value. it's not mandatory to create a constructor in a c# class. If a constructor is not defined in a class, the .Net framework provides a parameterless default constructor automatically.

When we create an object of a class, the constructor gets invoked.

Example:

public Student() //Constructor of Student class
{
 
}
 
public Student(string firstName, string lastName) //Parameterized Constructor of Student class
{
    _firstName = firstName;
    _lastName = lastName;
}

Method

Methods are also called functions. The methods in C# are very useful, which allow us to define the logics once and use them at many places. The maintenance of an application becomes very easy if methods are used.

Example :

public void PrintStudentDetail(string firstName, string lastName)  //Method or function, also called behavior of a class
{
    Console.WriteLine("Student first name is : {0} and student last name is : {1}", firstName, lastName);
}

Uses

In the following example, the Student class has been used to demonstrate that how you can create an object of a class, how to use properties and methods etc.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        public static void Main()
        {
            Student student = new Student(); // Default constructor called while create the object of Student class
 
            student.FirstName = "Jhon";  // Setting the values of different properties;
            student.LastName = "Doe";
            student.FullName = "Jhon Doe";
 
            student.PrintStudentDetail("Jhon", "Doe"); // calling PrintStudentDetail method of Student class using student object.
        }
    }
}
//Output
//Student first name is : Jhon and student last name is : Doe

Let's learn about the structs in the next section.