Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 62 - Creating partial classes in c#

Suggested Videos
Part 61 - Partial classes in c#

1. All the parts spread across different files, must use the partial keyword. Otherwise a compiler error is raised. 
Missing partial modifier. Another partial declaration of this type exists

2. All the parts spread across different files, must have the same access modifiers. Otherwise a compiler error is raised. 
Partial declarations have conflicting accessibility modifiers

3. If any of the parts are declared abstract, then the entire type is considered abstract.

4. If any of the parts are declared sealed, then the entire type is considered sealed

5. If any of the parts inherit a class, then the entire type inherits that class.



6. C# does not support multiple class inheritance. Different parts of the partial class, must not specify different base classes. The following code will raise a compiler error stating - Partial declarations must not specify different base classes.
public partial class SamplePartialClass : Employee
{
}
public partial class SamplePartialClass : Customer
{
}
public class Employee
{
}
public class Customer
{
}



7. Different parts of the partial class can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations. In the example below, SamplePartialClass needs to provide implementation for both IEmployee, and ICustomer interface methods.
public partial class SamplePartialClass : IEmployee
{
    public void EmployeeMethod()
    {
        //Method Implementation
    }
}
public partial class SamplePartialClass : ICustomer
{
    public void CustomerMethod()
    {
        //Method Implementation
    }
}
public interface IEmployee
{
    void EmployeeMethod();
}
public interface ICustomer
{
    void CustomerMethod();
}

8. Any members that are declared in a partial definition are available to all of the other parts of the partial class.

1 comment:

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.