Partial Class In C#

By
Partial Class:-
we can break a large class code into small pieces by specifying more than one class with same but with partial keyword. In that case those same name classes will not be treated as different classes so when we will made the object of the class, we will get all the method and member for all same name class.
  • Open the Console Application From Microsoft Visual Studio.(File->New Project->Console Application ->Enter the Name of Application(ex-partial class)->Program.cs).

  • Copy the below code and paste in Program.cs File.

Program code:-

using System;

namespace partial
{
    class Program
    {
        static void Main(string[] args)
        {
            int area;
            student obj = new student();
            obj.display();
            obj.show();
            obj.GetData(32, 12);
            area = obj.RectArea();
            Console.WriteLine("Area=" + area);
            Console.ReadLine();
        }
    }
   partial class student
    {
        public void display()
        {
            Console.WriteLine("welcome");
        }
    }
    partial class student
    {
        public void show()
        {
            Console.WriteLine("hello");

        }
    }
    partial class student
    {
        public int length, width;
        public void GetData(int x, int y)
        {
            length = x;
            width = y;
        }
        public int RectArea()
        {

            int area = length * width;
            return (area);
        }
    }
}

Output:-


Advantage of Partial Class:-
  • It is used to break one large class code into another class with same namespace but with partial keyword.
  • When our code is in compile form then we want to add an extra feature in class then we add extra feature by creating the partial class of the class in which we add extra code in separate but same namespace.
For More:-
I hope this is helpful for you.
To Get the Latest  Free Updates Subscribe
Click below for download whole application.
         DOWNLOAD

1 comment:

Powered by Blogger.