Different type of Classes in C-Sharp

Hello friends, today we are going to learn all types of available classes in C Sharp.

In C# We can create four types of classes

1) Abstract Class

2) Partial Class

3) Sealed Class

4) Static Class

1) Abstract classes:

Abstract classes, are declared by the keyword abstract in the class definition. It is used to create base class in inheritance. We can not create an object of an abstract class. We can create object of its derived class. We can create abstract, non-abstract and virtual methods of abstract class.

We can create abstract class as follows.

using System; namespace ConsoleApp3 { public abstract class StudentResult { public abstract int GetMarks(int a, int b); public int GetMarks(int a,int b, int c) { return a + b + c; } public virtual float GetResult(int TotalMarks, int ObtainedMarks) { return ((float)ObtainedMarks / (float)TotalMarks) * 100; } } public class StudentInfo : StudentResult { public override int GetMarks(int a, int b) { return a + b; } public override float GetResult(int TotalMarks, int ObtainedMarks) { float f = ((float)ObtainedMarks / (float)TotalMarks) * 100; if (f <= 33) return 0; else return 1; } } class Program { static void Main(string[] args) { StudentInfo s = new StudentInfo(); Console.WriteLine(s.GetMarks(56, 78)); int r = s.GetMarks(56, 78, 88); Console.WriteLine(r); Console.WriteLine(s.GetResult(300,r)); Console.ReadKey(); } } }

OUTPUT: 134 222 1

2) Partial Class:

Partial classes, are declared by the keyword partial in the class definition. Partial class is used for dividing their properties, methods and events into multiple physical source files with different name and at compile time these files are combined into a single class.

We can create Partial class as follows:

Create a file p1.cs and paste following code with Student Class and its properties.

namespace ConsoleApp3 { public partial class Student { //Properties public int StudentId { get; set; } public string StudentName { get; set; } } }

Create a file p2.cs and paste following code with Student class with constructor logic.

namespace ConsoleApp3 { public partial class Student { //Constructor public Student(int sid,string sname) { this.StudentId = sid; this.StudentName = sname; } } }

Create a file p3.cs and paste following code with Student class with member function logic.

namespace ConsoleApp3 { public partial class Student { //Methods public void GetStudentData() { Console.WriteLine($"Get Student Data Name: {this.StudentName} StudentId : {this.StudentId}"); } } }

Create program.cs file with console application and place all above three files into solution with following code.

using System; namespace ConsoleApp3 { class Program { static void Main(string[] args) { Student s = new Student(1,"Rohan"); s.GetStudentData(); Console.ReadKey(); } } }

OUTPUT: Get Student Data Name: Rohan StudentId : 1

3) Sealed Class:

Sealed classes, are declared by the keyword sealed in the class definition. A Sealed class is a class that cannot be inherited. we cannot use Access modifiers with sealed class.

we can create sealed class as follows.

public sealed class Student { }

4) Static Class:

Static classes, are declared by the keyword Static in the class definition.

We cannot create an object of that static class. We can create only static constructor with in static class. we can only use static members with in static class.

These classes cannot be instantiated. Their member function can be invoked by directly class name followed by period (.) sign.

We can create sealed class as follows.

using System; namespace ConsoleApp3 { public static class Student { public static int StudentId { get; set; } static Student() { } public static string GetResult(int TotalMarks) { return (TotalMarks > 33) ? "Pass" : "Fail"; } } class Program { static void Main(string[] args) { Student.StudentId = 12; Console.Write($"Student Id :{Student.StudentId} Result: {Student.GetResult(82)}"); Console.ReadKey(); } } }

OUTPUT: Student Id :12 Result: Pass

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories