Home > DeveloperSection > Blogs > Partial Class in C#

Partial Class in C#


C# C# 
Ratings:
0 Comment(s)
 1734  View(s)
Rate this:

Partial Class in C#

In this blog, I’m trying to explain the concept of partial class in c# and how to implement it.

A Partial class is one that can be split among multiple physical files. This feature was introduced with the release of C# version 2.0. With C#, we can split the source code for a class into separate files so that we can organize the definition of a large class into smaller, easier to manage pieces. When we split a class across multiple files, we define the parts of the class by using the partial keyword in each file. Each file contains a section of the class definition, and all parts are combined when the application is compiled.

Example


using System;

 

namespace PartialClassConsoleApplication

{

    public partial class Add

    {

        int x, y;

        public void getvalue(int x, int y)

        {

            this.x = x;

            this.y = y;

        }

    }

    public partial class Add

    {

        public void add()

        {

            Console.WriteLine("Addition of " + x + " & " + y + " = " + (x + y));

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Add aobj = new Add();

            aobj.getvalue(4, 6);

            aobj.add();

        }

    }

}

Output:-

Addition of 4 & 6 = 10

In this example, I’ve created a class named Add and split it into two parts with partial keyword. In one part , declared two variables x, y and assign value to them through function getvalue() and in other part created a function named add() who add the values of x and y and print the values.


Don't want to miss updates? Please click the below button!

Follow MindStick