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.
Let's talk about how classes help us in c#. There are the following type of classes available in c#
- Static Class
- Sealed Class
- Partial Class
- Abstract Class
$ads={1}
Static Class:
- Static classes are sealed and therefore cannot be inherited.
- Contains only static members and static constructor.
- Static variables are stored in the heap memory.
- You can access the members of a static class by using the class name itself.
- 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:
- We can create an instance of the sealed class in c#.
- 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:
- Each part of a partial class should be in the same assembly or DLL.
- A partial class is only possible in the same namespace and same assembly.
- The partial class must be prefixed with the partial keyword.
- 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:
Some Key Points About Abstract Class
- We can't be instantiated.
- Abstract classes are used to achieve abstraction in C#
- It can have abstract and non-abstract methods.
- 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;
}
}
Post a Comment