classes and constructors in C#

Classes and Constructors in C# Made Easy For You in 2021

Classes and Constructors in C# – Intro

In this article, we are going to cover one of the important topics of any object-oriented programming language – classes and constructors in C#.

Along with classes and constructors, we will also cover types of classes and constructors in C#.

Let’s start with the basics.

What is a Class?

Class is a blueprint of an object. So, if we go with a real-world example, every object has its own features like color, shapes, characteristics, etc.

 A class is a user-defined data type. It is of a reference type.

In C#, a class can be defined using a class keyword:

public class classname
{
     // variable declaration
     .
     .
     // methods
}

Classes contain members such as variables, constructors, properties, and methods. In order to access the members of a class, we need to create objects using the new keyword.

Employee emp = new Employee();

emp.GetEmployeeDetails(1,"DotNet Crunch");

Object emp has an access to the members of the class Employee. Using the dot operator (.), we can access the class members.

Example of a class in C#:

public class Employee
{
    public string employeeId = string.Empty;

    public Employee()
    {

    }

    public void GetEmployeeDetails(int id, string name)
    {
        Console.WriteLine("ID: {0},Name: {1}",id, name);
    }

    public int MyAutoImplementedProperty { get; set; }

    private int myPropertyVar;

    public int MyProperty
    {
        get { return myPropertyVar; }
        set { myPropertyVar = value; }
    }
}

When we create a new project in a Visual Studio, a class named “Program” is created:

 

Classes and Constructors in C#

Classes in C#

There are different types of classes we can create based on our requirements. Let us see a few of them:

Abstract Class

An abstract class is one in which you cannot create an object.

An abstract class can be used in a situation where we want to act our class as a base class and don’t want anyone to create its object.

In .NET, Expression is an abstract class that contains static methods and inherited by various types.

Abstract Class in C# Example:

abstract class Base
{

}

Class Derived: Base
{

}

Partial Class

A partial class as the name suggests is not a complete class.

When a situation arises that two or more developers need to work on the same class file, we may create a partial class and every developer could work independently.

Furthermore, we can create a partial method inside those classes as well.

Partial Classes in C# Example:

partial class Employee
{
   private string _firstName;
   private string _lastName;

   public Employee()
   {
     _firstName = “Dipendra”;
     _lastName = “Shekhawat”;
   }
}

 
partial class Employee
{
   public string GetFullName()
   {
      return _firstName + ' ' + _lastName;
   }
}

Sealed Class

When we want to prevent our class from being inherited by another class for whatever reason, we would mark our class as sealed.

Sealed Class in C# Example:

Sealed class A
{

}

Sealed class B: C
{

}

Also, a sealed class cannot be an abstract class.

What is Constructor?

Constructor in C# is a special method for initializing the class members.

Constructors are always public by default and have the same name as that of a class. They do not have a return type, not even void:

class Employee
{
   public Employee(int x, string y)
   {
      id = x;
      name = y;
   }
}

Constructors in C#

In C#, Constructors are usually public. However, they can also be declared private or protected.

Private Constructor

In many situations, we may wish to define some utility classes that contain only static members. Such classes are never required to instantiate objects. We can mark the constructor as private for such cases.

Static Constructor

A static constructor is called before any object of the class is created. It is declared by prefixing a static keyword to the construction definition.

A class can only have one static constructor and there is no access modifier on static constructors:

class Student
{
  static Student ()
  {
     // some code
  }
}

Copy Constructor

A copy constructor creates an object by copying variables from another object.

Since C# does not provide a copy constructor, we must declare it ourselves if we wish to add this feature to the class.

Further Reading on Classes and Constructors in C#

In order to know more in-depth about classes and constructors in C# you may refer to these links from Microsoft’s Documentation:

Bonus – Hello World in C#

This is the Hello World program in C# to get you started. The components of a hello world program are:

  • Using declaration
  • Namespace
  • Class
  • Method
using System;

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");    
    }
  }
}

Classes and Constructors in C# – Summary

To summarize, in this article, we have learned:

  • About classes and their usage
  • Types of classes
  • Constructors and the different types of constructors

⭐C# Mastery – Mosh Hamedani

One of the best courses to master your C# skills right from level zero. This series is divided into 4 sections:

  1. C# Basics
  2. C# Intermediate
  3. C# Advanced
  4. Unit Testing for C# Developers

csharp mastery course mosh

I hope you now have a good understanding of Classes and Constructors in C#. Please share this post with your developer friends.

Share with your friends:

Leave a Comment

Your email address will not be published. Required fields are marked *