Types of Classes in C# With Example

Types of Classes in C# With Example: In this session, we will talk about the different types of classes in c#. These classes help us to reuse the code and concepts of class. Different type of classes plays a major role in Application Development.

Types of Classes in C# With Example


Let's talk about how classes help us in c#. There are the following type of classes available in c#

  1. Static Class
  2. Sealed Class
  3. Partial Class
  4. Abstract Class
Note: In C#, By default Class is internal

$ads={1}

Static Class:

Static Classes are created using a static keyword, If any class marked as static it means we can't create an object of that class and the compiler will guarantee that instances (object) of this class cannot be created., just why because static classes are loaded automatically by the .NET Framework common language runtime (CLR).

Some Key Points About Static Class
  1. Static classes are sealed and therefore cannot be inherited.
  2. Contains only static members and static constructor.
  3. Static variables are stored in the heap memory.
  4. You can access the members of a static class by using the class name itself. 
  5. Static class can be public.

Static Class Example



using System;

using System.Collections.Generic;

using System.Text;

 

namespace CSharpClasses

{

    public static class College

    {

        // Static Data Members of College

        public static string Name = "LPS Inter College";

        public static string Address = "Noida Sctor 62";

        public static int PhoneNo = 000;

        static College() // Static Constructor

        {

 

        }

        public static string TimeTable() // Static Function

        {

            string timeT = "Morning";

            return timeT;

        }

    }

    public class MainClass

    {

        // Main Method

        static public void Main()

        {

            //Access the Static class Member By Class Name itself

            Console.WriteLine("Name : {0} ", College.Name);

            Console.WriteLine("Address : {0} ", College.Address);

            Console.WriteLine("PhoneNo : {0} ", College.PhoneNo);

            Console.WriteLine("TimeTable : {0} ", College.TimeTable());

        }

    }

}


Sealed Class:

A sealed class is a class that does not allow inheritance. Some object model designs need to allow the creation of new instances but not inheritance, if this is the case, the class should be declared as sealed. To create a sealed class in C#, the class declaration should be done as:

Some Key Points About Static Class

  1. We can create an instance of the sealed class in c#.
  2. We can not inherit the sealed class.

Sealed Class Example

using System;
 
namespace Abstact_Class
{
    public abstract class abstactClass
    {
        //abstract method
        abstract public void abstractMethod();
 
        //normal method with implementation
        public void normalMethod()
        {
            Console.WriteLine("Normal method impemented in abstract class");
        }
    }
 
    public class derivClass:abstactClass
    {
 
        public override void abstractMethod()
        {
            Console.WriteLine("abstract method implemented in derived class");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            derivClass derivCls = new derivClass();
            derivCls.abstractMethod();
            derivCls.normalMethod();
 
            Console.ReadLine();
        }
    }
}


 $ads={2}

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.

When we work on a large project and write the code in a single .cs file. It is possible to include more than one type in a single class file but a single type's code cannot be split into sections into multiple separate files. That's why, Microsoft decided to overcome the above issue, with the introduction of something called partial types.

Some Key Points About Static Class

  1. Each part of a partial class should be in the same assembly or DLL.
  2. A partial class is only possible in the same namespace and same assembly.
  3. The partial class must be prefixed with the partial keyword.
  4. We can apply inheritance on partial class.
using System;
 
namespace partial_class_and_methods
{
    //partial class part1
    partial class partialClass
    {
        partial void ShowMessage(string message);
    }
 
    //partial class part2
    partial class partialClass
    {
        partial void ShowMessage(string message)
        {
            Console.WriteLine("Partial method declaration with message" + message );
        }
    }
}


Abstract Class:

An abstract is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

Some Key Points About Abstract Class

  1. We can't be instantiated. 
  2. Abstract classes are used to achieve abstraction in C#
  3. It can have abstract and non-abstract methods.
  4. It cannot be instantiated. 

Abstract Class Example

abstract class Shape

   {

        public abstract double GetArea();

        public abstract double GetPerimeter();

   }

  class Rectangle : Shape

   {

       private double width;

       private double height;

       public Rectangle(double width, double height)

       {

           this.width = width;

           this.height = height;

        }

        public override double GetArea()

       {

           return width * height;

        }

        public override double GetPerimeter()

        {

            return 2 * (width + height);

        }

   }

   class Circle : Shape

   {

       private double radius;

       public Circle(double radius)

       {

           this.radius = radius;

       }

       public override double GetArea()

       {

           return Math.PI * radius * radius;

       }

       public override double GetPerimeter()

       {

          return 2 * Math.PI * radius;

        }

   }



In this example, the abstract class Shape defines two abstract methods GetArea() and GetPerimeter() that must be implemented by any derived classes. The Rectangle class and Circle class both inherit from the Shape class and provide an implementation for both GetArea() and GetPerimeter() methods. The abstract class Shape can't be instantiated, but it serves as a common base for the derived classes Rectangle and Circle.

This example demonstrates how the abstract class Shape defines a contract that must be implemented by the derived classes and provides a common base for them, while the derived classes Rectangle and Circle provide the implementation details for the methods defined in the abstract class Shape.

Post a Comment

Post a Comment (0)

Previous Post Next Post