Partial Class


Partial Class
  • A class which code can be written in two or more files is known as Partial Class
  • To make any class as partial use partial keyword
  • Partial classes will provide flexibility in Application Development
  • It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled. There are several situations when splitting a class definition is desirable
  • When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously
  • When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating windows forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio

Example :-

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

namespace CAParcial
{
    partial class ClsEmployee
    {
        int EmpId,EAge;
        string EName,EAddress;
        public void GetEData()
        {
            Console.WriteLine("Enter Employee Details   :-");
            EmpId = Convert.ToInt32(Console.ReadLine());
            EName = Console.ReadLine();
            EAge = Convert.ToInt32(Console.ReadLine());
            EAddress= Console.ReadLine();
        }
    }
    class ClsPartial
    {
        static void Main(string[] args)
        {
            ClsEmployee obj1 = new ClsEmployee();
            obj1.GetEData();
        }
    }
}



Create a new class with the name ClsSample and write the following

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

namespace CAParcial
{
    class ClsSample
    {
    }
    partial class ClsEmployee
    {
        public void DisplayEmpData()
        {
            Console.WriteLine("Employee Id is       :-  "+EmpId);
            Console.WriteLine("Employee Name is     :-  "+EName);
            Console.WriteLine("Employee Age is      :-  " + EmpId);
            Console.WriteLine("Employee Address is  :-  " + EName);
        }
    }
}



Output :-



No comments:

Post a Comment