Introduction
In this article, we will understand the types of classes in C#. There are four different types of classes available in C#. They are as follows:
- Static class
- Abstract class
- Partial class
- Sealed class
Static class in C#
A class with a static keyword that contains only static members is defined as a static class. A static class cannot be instantiated.
Characteristics of static class in C#
- Static class cannot be instantiated using a new keyword.
- Static items can only access other static items. For example, a static class can only contain static members, e.g., variables, methods, etc.
- A static method can only contain static variables and can only access other static items.
- Static items share the resources between multiple users.
- Static cannot be used with indexers, destructors, or types other than classes.
- A static constructor in a non-static class runs only once when the class is instantiated for the first time.
- A static constructor in a static class runs only once when any of its static members are accessed for the first time.
- Static members are allocated in a high-frequency heap area of the memory.
Example
using System;
namespace StaticClass_Demo
{
public static class HeightConvertor
{
public static double InchsToCentimeters(string HeightInInchs)
{
double inchs = Double.Parse(HeightInInchs);
double Centimeters = (inchs* 2.54);
return Centimeters;
}
public static double CentimetesToInchs(string HeightInCentimeters)
{
double centimeters = Double.Parse(HeightInCentimeters);
double Inchs = (centimeters / 2.54);
return Inchs;
}
}
}
using System;
namespace StaticClass_Demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please select the convertor direction");
Console.WriteLine("1. From Inchs to Centimeters.");
Console.WriteLine("2. From Centimeters to Inchs.");
string selection = Console.ReadLine();
double C, I = 0;
switch (selection)
{
case "1":
Console.Write("Please enter the height in inchs: ");
C = HeightConvertor.InchsToCentimeters(Console.ReadLine());
Console.WriteLine("Hieght in centimeters: {0:F2}", C);
break;
case "2":
Console.Write("Please enter the Height in centimeters: ");
I = HeightConvertor.CentimetesToInchs(Console.ReadLine());
Console.WriteLine("Height in Inchs: {0:F2}", I);
break;
default:
Console.WriteLine("Please select a convertor.");
break;
}
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
}
Output


Abstract class in C#
A class with an abstract modifier indicates that the class is abstract. An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
Characteristics of Abstract class
- An abstract class cannot be instantiated.
- An abstract class may contain abstract methods and accessors.
- Modifying an abstract class with the sealed modifier is impossible because the two modifiers have opposite meanings. The sealed modifier prevents a class from being inherited, and the abstract modifier requires a class to be inherited.
- A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.



Pratyush BeheraPosted Nov 13, 2018, 11:04 PM
4th point of Characteristics of Partial Class - "Different parts can have different base types and so the final class will inherit all the base types.". So will this not violate the multiple inheritance concept?