Ads

C# Programming 15 - Partial Class

Hello All,

In this article we will learn about partial class in C#.

What is Partial Class?

A Class which code can be written into two or files called Partial Class.

With the help of partial keyword we can make any class as partial.

With the help of partial concept it is possible to split the definition of class over two or more source files.


It is very helpful when we work on large project, spreading a class over separate files allows multiple developers to work on it simultaneously.

Note: Each source files contain a section of the class definition, and all parts are combined when the application is compiled.

Here is the Simple Example for Partial Class.

1. Lets Create console application

2. Add Class called Learning and mark it as partial

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public partial class Learning
    {
        public void GetCourese()
        {
            Console.WriteLine("All Course Display Here...");
        }
    }
}


3. Lets add one more class called LearningSplit because we are splitting the Learning class functionality to LearningSplit and mark this class as partial

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public partial class Learning
    {
        public void GetCategory()
        {
            Console.WriteLine("All Category Display Here....");
        }
    }
}


4. When we create the object of the class Learning, we get both the methods GetCourse and Get Category


5. Program.cs contains below code

using ConsoleApp1;

Learning obj=new Learning();

obj.GetCategory();
obj.GetCategory();

Console.ReadKey();

Lets run the application and out is below.


That's it for this article, see you in next article. Take care Bye

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !