Introduction
As we know, C# is an Object Oriented Programming language that provides the ability to reuse existing code. To reuse existing code, C# provides various object-oriented concepts such as classes, objects, properties, methods, structs, and records.
Now in this article, we learn about the overview of classes that are part of C# Object Oriented Programming concepts, and in the next article, we learn in detail about each class in depth. Let us start with defining the class.
What is a class in C#?
Classes are user-defined data types that represent the state and behavior of an object. The state represents the properties, and behavior is the action that objects can perform.
Classes can be declared using the following access specifiers that limit the accessibility of classes to other classes. However, some classes do not require any access modifiers.
- Public
- Private
- Protected
- Internal
- Protected internal
To learn the details of access specifiers, check out Access Modifiers in C#.
For example
public class Accounts
{
}
Some Key points about classes:
- Classes are reference types that hold the object created dynamically in a heap.
- All classes have a base type of System.Object.
- The default access modifier of a class is Internal.
- The default access modifier of methods and variables is Private.
- Directly inside the namespaces, declarations of private classes are not allowed.
Types of classes in C#
What is an Abstract Class in C#?
An abstract class is a class that provides a common definition to the subclasses, and this is the type of class whose object is not created.
Some key points of Abstract classes are:
- Abstract classes are declared using the abstract keyword.
- We cannot create an object of an abstract class.
- It must be inherited in a subclass if you want to use it.
- An Abstract class contains both abstract and non-abstract methods.
- The methods inside the abstract class can either have an or no implementation.
- We can inherit two abstract classes; in this case, implementation of the base class method is optional.
- An Abstract class has only one subclass.
- Methods inside the abstract class cannot be private.
- If there is at least one method abstract in a class, then the class must be abstract.
For example
abstract class Accounts
{
}
Continue here: Abstract Classes In C# (c-sharpcorner.com)
What are Partial Classes in C#
It is a type of class that allows dividing their properties, methods, and events into multiple source files, and at compile time, these files are combined into a single class.
The following are some key points:
- All the parts of the partial class must be prefixed with the partial keyword.
- If you seal a specific part of a partial class, the entire class is sealed, the same as for an abstract class.
- Inheritance cannot be applied to partial classes.
- The classes written in two class files are combined at run time.
For example
partial class Accounts
{
}
Continue here: Partial Classes in C#(c-sharpcorner.com)
What is Sealed Class in C#?
A Sealed class is a class that cannot be inherited and used to restrict the properties.
The following are some key points:
- A Sealed class is created using the sealed keyword.
- Access modifiers are not applied to a sealed class.
- To access the sealed members, we must create an object of the class.
For example
sealed class Accounts
{
}
Continue reading: Sealed Class in C# (c-sharpcorner.com).
What is Static Class in C#?
It is the type of class that cannot be instantiated. In other words, we cannot create an object of that class using the new keyword, such that class members can be called directly using their name.
The following are some key points:
- It was created using the static keyword.
- Only static members are allowed; in other words, everything inside the class must be static.
- We cannot create an object of the static class.
- A Static class cannot be inherited.
- It allows only a static constructor to be declared.
- The static class methods can be called using the class name without creating the instance.
For example
static class Accounts
{
}
Continue reading Static Class In C# (c-sharpcorner.com).
Summary
I hope you have learned an overview of various types of classes in C#. In the next article, we will learn each class in C#.

Md Safikul IslamPosted Sep 6, 2024, 6:31 PM
Many Many thanks sir
vaibhav HedaooPosted Jul 29, 2023, 6:34 AM
For abstarct class, We can inherit two abstract classes; in this case, implementation of the base class method is optional, this statement is totally wrong, please explain further.
DIPAK AKHADEPosted Dec 10, 2021, 2:35 PM
I can inherit class from and by partial class in C# 7.3 "Inheritance cannot be applied on partial classes."
Sami KhanPosted Sep 16, 2021, 2:11 PM
Thanks for sharing your knowledge. Its very helpful.
Dinesh KudalePosted Feb 8, 2021, 12:33 PM
Thank you for sharing your knowledgeable & valuable article with us.
Masthan BPosted Apr 6, 2019, 10:32 AM
In general for a class we can apply public or internal. Access modifiers are not applied to a sealed class . this is wrong. we can apply access modifiers to the sealed class. if not can you please explain. how ?. i having doubt on this sentence.
Arjun ApsundePosted Dec 3, 2018, 9:35 PM
Methods inside the abstract class cannot be private.......this is wrong.....Non abstract Methods inside the abstract class can be private.
Arjun ApsundePosted Dec 3, 2018, 9:20 PM
An Abstract class has only one subclass....What is this?...I think we can inherit abstract class on n number of subclasses
Chandrakant VegadPosted Nov 25, 2018, 11:38 PM
"Inheritance cannot be applied on partial classes." - Can you please explain?
Intekhab KhanPosted Apr 2, 2017, 8:45 AM
Can we use access specifier with class rather than public and internal?
SubashPosted Mar 8, 2017, 1:27 AM
Very nice explanation useful share
Ali RahmanPosted Aug 14, 2014, 8:35 AM
using System; /* Author : Alfred ,Ali,Jude Date : July 14,August,2014 Purporse : Move Grid Puzzle.This has table of values with random and one blank cell we have to arrange it in order. Input : 0 5 8 7 4 3 2 6 1 Output : 1 2 3 4 5 6 7 8 0 */ class MoveGridPuzzle { private int[,] Grid=new int[3,3]; private string _ValidMove; private int CurRow,CurCol; public MoveGridPuzzle() { Reset(); } // Default Grid Position at Start/Replay of Game public void Reset() { Grid[0,0]=0;Grid[0,1]=5;Grid[0,2]=8; Grid[1,0]=7;Grid[1,1]=4;Grid[1,2]=3; Grid[2,0]=2;Grid[2,1]=6;Grid[2,2]=1; CurRow=0;CurCol=0; SetValidMove(); } // Check if the Player is Won public bool IsWon() { for(int i=Grid.GetLowerBound(0);i<=Grid.GetUpperBound(0);i++) { for(int j=Grid.GetLowerBound(1);j<=Grid.GetUpperBound(1);j++) { // If the Last Position is zero and remaining position // are valid then return true if(i==Grid.GetUpperBound(0) && j==Grid.GetUpperBound(1)) { return true; } else { if(Grid[i,j]!=(3*i)+j+1) { return false; } } } } return true; } // Check if the Move performed is valid or not private bool IsMoveValid(string Move) { return (ValidMove.IndexOf(Move))>=0?true:false; } // Property to get and set ValidMove private string ValidMove { get { return _ValidMove; } set { _ValidMove=value; } } //Function to set Valid Move Position from Current Position private void SetValidMove() { string Move; switch(CurRow) { case 0: Move="D"; break; case 1: Move="UD"; break; case 2: Move="UD"; break; default: Move=""; break; } switch(CurCol) { case 0: Move+="R"; break; case 1: Move+="LR"; break; case 2: Move+="L"; break; default: Move=""; break; } ValidMove=Move; } // Set Value to the Grid Position private int this[int row,int col] { get { return Grid[row,col]; } set { Grid[row,col]=value; } } //Perform Move here.Change Grid Position Values if the Move is Valid private void Play() { bool IsNotGameDone=true; string MoveTo; do { ShowGrid(); Console.Write("Enter your Move [L,R,U,P,E]: "); MoveTo=Console.ReadLine().ToUpper(); if(IsWon()) { Console.WriteLine("You Won the Game!!"); MoveTo="E"; } if(IsMoveValid(MoveTo)) { switch(MoveTo) { case "L": case "l": SwapPosition(CurRow,CurCol,CurRow,CurCol-1); break; case "R": case "r": SwapPosition(CurRow,CurCol,CurRow,CurCol+1); break; case "U": case "u": SwapPosition(CurRow,CurCol,CurRow-1,CurCol); break; case "D": case "d": SwapPosition(CurRow,CurCol,CurRow+1,CurCol); break; } SetValidMove(); } else { switch(MoveTo) { case "E": case "e": IsNotGameDone=false; if(!IsWon()) { Console.WriteLine("You Lose the Game"); } break; default: Console.WriteLine("Invalid Move or Choice"); break; } } }while(IsNotGameDone); } //Show Grid private void ShowGrid() { Console.WriteLine("+===+===+===+"); for(int i=Grid.GetLowerBound(0);i<=Grid.GetUpperBound(0);i++) { Console.Write("+"); for(int j=Grid.GetLowerBound(1);j<=Grid.GetUpperBound(1);j++) { Console.Write(" {0} +",this[i,j]); } Console.WriteLine(); } Console.WriteLine("+===+===+===+"); } // Reassign the Grid Value after the move private void SwapPosition(int FromRow,int FromCol,int ToRow,int ToCol) { int Temp; Temp=this[FromRow,FromCol]; this[FromRow,FromCol]=this[ToRow,ToCol]; this[ToRow,ToCol]=Temp; CurRow=ToRow; CurCol=ToCol; } public static void Main() { MoveGridPuzzle GameObject=new MoveGridPuzzle(); GameObject.Play(); } }
Ali RahmanPosted Aug 14, 2014, 8:08 AM
I want to create a class in a certain program that will count the moves in the game.here is the source code
Vithal WadjePosted Jun 13, 2014, 5:02 AM
sure,wait i will update my article soon
manikant kumarPosted Jun 11, 2014, 2:44 AM
We can inherit two abstract classes; in this case the base class method implementation is optional...what is this???explain it plz...