What is the partial class?

Partial class allows us to split the definition of a class or a sruct or an interface over two or more source files.Each source file contains a section of the class definition and all the parts are grouped as a single class when the application compiled.
So the main advantage is,it will allow more than one developer can work on the same class.
   
    Entire class definition in one file (example.cs)
    public class Employee
    {
        public void Add(){
        //some code
        }   
        public void Delete(){
        //some code
        }   
    }

    if we use the partial for the above class
  
    //example1.cs
    public partial class Employee
    {
        public void Add(){
        //some code
        }   
    }

    //example2.cs
    public partial class Employee
    {
        public void Delete(){
        //some code
        }   
    }

    We should be careful,when writing code for partial classes....
    1)All the partial definitions of a class or a struct or an interface must proceeded with the keyword "partial".
    2)The partial types must have the same accessibility.
    3)If any part is declared as sealed,then the entire class is sealed.
    4)If any part is declared as abstract,then the entire class is abstract.

Home

No comments:

Post a Comment