In this article I am going to discuss about C# types of classes in detail but before that if you had not read my previous article of C# Classes and objects then must read it as this article is continuation of that article. In C# there are so many types of classes are there that is used as per the requirement of program. Among all the classes abstract classes are very common to use. If you know the types of classes in csharp then you can easily decide which type of class is suitable for any situation.

Types of Classes in C#

csharp types of classes

1. Abstract Class

In C#, Abstract Class helps you to create member function that does not have implementation but must be implemented in the derived class. Here implementation means program body. We can implement the abstract methods using override keyword in derived class. Abstract class can be created using abstract keyword. We cannot create object of abstract class because abstract class’s method may not contain implementation. In abstract class we can create methods with implementation also and without implementation also. If we want to create method without implementation then we must have to use the abstract keyword. In simple words abstract class can contain both abstract methods and non-abstract methods. Remember methods inside abstract class cannot be private.

We can declare abstract methods in the abstract class only. If we are are creating one abstract class in normal class then it throw an error.

public abstract class Accounts
{
   //methods
}
// abstract class with abstract and non-abstract methods

public abstract class Accounts
{
  public void Sum(){
     return 10+20;
  }
  public abstract void Multiply(int x, int y);   //abstract method does not have implementation
}

public class ManageAccounts : Accounts
    {
        public override void Multiply(int x, int y)
        {
           Console.WriteLine($"Multiplication of {x} and {y} is : {x * y}");
        }
    }

2. Static Class

In C# among types of classes static class is one of the important class. The static class can be created using keyword static. A static class can only contain static members and like abstract classes we cannot create the instance of static class. We can access the static class members using directly with its class name. Remember we cannot inherit the static class.

namespace StaticClassDemo
{
    public static class CommonTask
    {
        public static int GetSumOfTwoNumber(int first, int second)
        {
            return first+second;
        }
    }
}


//For accessing GetSumOfTwoNumber method we can do like that
  int result= CommonTask.GetSumOfTwoNumber(10,20);   //accessing static method with class name

3. Sealed Class

In C#, among different types of classes sealed class is used to create sealed class which means the class which is not inherited by any other class. This class is especially created for the purpose of not being inherited by any other class. Sealed class can be created using sealed keyword. Like sealed class there is sealed method which is used to stop overriding the method in derived classes. Sealed methods can be declared in normal classes. Remember sealed class does not contain abstract methods and we can create objects of sealed class.

namespace SealedClassDemo
{
    public sealed class CommonTask   //Now this class is not inherited by any other class
    {
        public int GetSumOfTwoNumber(int first, int second)
        {
            return first+second;
        }
    }
}

4. Partial Class

In C#, among types of classes partial class is used to divide the properties of one class into multiple source files but at the compile time all these source files are grouped together to make one single class. For creating partial class we need to use the keyword partial. This type of class is important when your class contains lots of properties and you want to separate the properties into small parts. Let see the partial class with an example

using System;
namespace NormalClass
{
    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public double Salary { get; set; }

        public void StudentFullName(){
           Console.WriteLine($"Full Name of Student is : {FirstName} {LastName}");
        }
     
    }
}

In above code we have simply created one class with name student and display their properties and methods. But if we have to do the same thing with partial class then we need to create two (depends on your choice I have chose 2) separate files with the same class name. Like I have created two files with name StudentDetail1.cs and StudentDetail2.cs but both files should contain the class with same name. For example see the below code :

//File name is StudentDetail1.cs
using System;
namespace PartialClassDemo       
{
    public partial class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public double Salary { get; set; }
     
    }
}


//File name is StudentDetail2.cs
using System;
namespace PartialClassDemo       
{
    public partial class Student
    {
        public void StudentFullName(){
           Console.WriteLine($"Full Name of Student is : {FirstName} {LastName}");
        }
     
    }
}

You can see that I have break the one class into two different-2 classes with creating partial class and you must have noticed that I can use the properties of StudentDetail1.cs class properties FirstName and LastName in StudentDetail2.cs file because we are using the same class. This is the power of partial class. Now if you want to access the Student detail. simply create the instance of Student Class and you can access both the properties and methods with object.

5. Concrete Class

In C# among different types of classes concrete class is last type of class which must have an implementation for all of its methods. It is a opposite of abstract class but it can extend an abstract class and implement the abstract methods of an abstract class. In simple words concrete class is simply a normal class. For example :

using System;
namespace NormalClass
{
    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public double Salary { get; set; }

        public void StudentFullName(){
           Console.WriteLine($"Full Name of Student is : {FirstName} {LastName}");
        }
     
    }
}

Conclusion

In this C# tutorials article I have tried to discuss all the types of classes in csharp programming language with example. Hope you understand all the types of classes in C# and found this article helpful. Hope you like this article. In my next article I am going to discuss about constructors.