Different Types Of Classes In C#

In this article. We will understand types of classes in c#. There are four different type of classes available in c#.

Introduction

In this article we will understand types of classes in C#. There are four different types of classes available in C#. They are as follows:

1. Static class
2. Abstract class
3. Partial class
4. Sealed class

Static class

A class with static keyword that contains only static members is defined as static class. A static class cannot be instantiated.

Characteristics of static class

  1. Static class cannot instantiated using a new keyword.
  2. Static items can only access other static items. For example, a static class can only contain static members, e.g. variable, methods etc.
  3. A static method can only contain static variables and can only access other static items.
  4. Static items share the resources between multiple users.
  5. Static cannot be used with indexers, destructors or types other than classes.
  6. A static constructor in a non-static class runs only once when the class is instantiated for the first time.
  7. A static constructor in a static class runs only once when any of its static members accessed for the first time.
  8. Static members are allocated in a high frequency heap area of the memory.

Example

  1. using System;  
  2.   
  3. namespace StaticClass_Demo  
  4. {  
  5.     public static class HeightConvertor  
  6.     {  
  7.         public static double InchsToCentimeters(string HeightInInchs)  
  8.         {  
  9.             double inchs = Double.Parse(HeightInInchs);  
  10.             double Centimeters = (inchs* 2.54);  
  11.             return Centimeters;  
  12.         }  
  13.   
  14.         public static double CentimetesToInchs(string HeightInCentimeters)  
  15.         {  
  16.             double centimeters = Double.Parse(HeightInCentimeters);  
  17.             double Inchs = (centimeters / 2.54);  
  18.             return Inchs;  
  19.         }  
  20.     }  
  21. }  
  1. using System;  
  2.   
  3. namespace StaticClass_Demo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("Please select the convertor direction");  
  10.             Console.WriteLine("1. From Inchs to Centimeters.");  
  11.             Console.WriteLine("2. From Centimeters to Inchs.");  
  12.   
  13.             string selection = Console.ReadLine();  
  14.             double C, I = 0;  
  15.   
  16.             switch (selection)  
  17.             {  
  18.                 case "1":  
  19.                     Console.Write("Please enter the height in inchs: ");  
  20.                     C = HeightConvertor.InchsToCentimeters(Console.ReadLine());  
  21.                     Console.WriteLine("Hieght in centimeters: {0:F2}", C);  
  22.                     break;  
  23.   
  24.                 case "2":  
  25.                     Console.Write("Please enter the Height in centimeters: ");  
  26.                     I = HeightConvertor.CentimetesToInchs(Console.ReadLine());  
  27.                     Console.WriteLine("Height in Inchs: {0:F2}", I);  
  28.                     break;  
  29.   
  30.                 default:  
  31.                     Console.WriteLine("Please select a convertor.");  
  32.                     break;  
  33.             }  
  34.   
  35.             Console.WriteLine("Press any key to exit.");  
  36.             Console.ReadLine();  
  37.         }  
  38.     }  
  39. }  

Output

Types Of Classes Of In C#

Types Of Classes Of In C# 

Abstract class

A class with abstract modifier indicate that class is abstract class. 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.

Characteristic of Abstract class

  1. An abstract class cannot be instantiated.
  2. An abstract class may contain abstract methods and accessors.
  3. It is not possible to modify an abstract class with the sealed modifier 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.
  4. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

Example

  1. using System;  
  2.   
  3. namespace AbstractClass_demo  
  4. {  
  5.     public abstract class Customer  
  6.     {  
  7.         private string _firstName;  
  8.         private string _lastName;  
  9.   
  10.         public string FirstName  
  11.         {  
  12.             get  
  13.             {  
  14.                 return _firstName;  
  15.             }  
  16.   
  17.             set  
  18.             {  
  19.                 _firstName = value;  
  20.             }  
  21.         }  
  22.   
  23.         public string LastName  
  24.         {  
  25.             get  
  26.             {  
  27.                 return _lastName;  
  28.             }  
  29.   
  30.             set  
  31.             {  
  32.                 _lastName = value;  
  33.             }  
  34.         }  
  35.   
  36.         public abstract void FullName();  
  37.     }  
  38. }  
  1. using AbstractClass_Demo;  
  2. using System;  
  3.   
  4. namespace AbstractClass_Demo  
  5. {  
  6.     class Program: Customer  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             Program p = new Program();  
  11.             p.FirstName = "Farhan";  
  12.             p.LastName = "Ahmed";  
  13.             p.FullName();  
  14.             Console.ReadLine();  
  15.              
  16.         }  
  17.         public override void FullName()  
  18.         {              
  19.             Console.WriteLine("Full Name:"+FirstName + " " +LastName);  
  20.         }  
  21.     }  
  22. }  

Output

Types Of Classes Of In C#

Partial class

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.

Characteristic of Partial class

  1. All the partial class definitions must be in the same assembly and namespace.
  2. All the parts must have the same accessibility like public or private, etc.
  3. If any part is declared abstract, sealed or base type then the whole class is declared of the same type.
  4. Different parts can have different base types and so the final class will inherit all the base types.
  5. The Partial modifier can only appear immediately before the keywords class, struct, or interface.
  6. Nested partial types are allowed.

Example

  1. using System;  
  2.   
  3. namespace PartialClass_Demo  
  4. {  
  5.     public partial class PartialClass  
  6.     {  
  7.         private string _firstName;  
  8.         private string _lastName;  
  9.         public string FirstName  
  10.         {  
  11.             get  
  12.             {  
  13.                 return _firstName;  
  14.             }  
  15.   
  16.             set  
  17.             {  
  18.                 _firstName = value;  
  19.             }  
  20.         }  
  21.         public string LastName  
  22.         {  
  23.             get  
  24.             {  
  25.                 return _lastName;  
  26.             }  
  27.   
  28.             set  
  29.             {  
  30.                 _lastName = value;  
  31.             }  
  32.         }  
  33.     }  
  34. }  
  1. using System;  
  2.   
  3. namespace PartialClass_Demo  
  4. {  
  5.     public partial class PartialClass  
  6.     {  
  7.         public void FullName()  
  8.         {  
  9.             Console.WriteLine("Full Name:"+FirstName+" "+LastName );  
  10.         }  
  11.     }  
  12. }  
  1. using System;  
  2.   
  3. namespace PartialClass_Demo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             PartialClass partial = new PartialClass();  
  10.             partial.FirstName = "Farhan";  
  11.             partial.LastName = "Ahmed";  
  12.             partial.FullName();  
  13.   
  14.             Console.ReadLine();  
  15.         }  
  16.     }  
  17. }  

Output

Types Of Classes Of In C#

Sealed class

A class with sealed keyword indicates that class is sealed to prevent inheritance. Sealed class cannot inheritance.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace SealedClass_Demo  
  8. {  
  9.     public sealed class Employee  
  10.     {  
  11.         string firstName;  
  12.         string lastName;  
  13.     }  
  14.     class Program:Employee  
  15.     {  
  16.         static void Main(string[] args)  
  17.         {  
  18.   
  19.         }  
  20.     }  
  21. }  
Types Of Classes Of In C#