C# Partial Class

  Keywords : partial class in c#, c# partial, c# public partial class, c# create partial class, partial methods in c#, what is a partial class in c#

Here we will learn what is partial class in c# with examples and how to use c# partial classes to split the functionality of single class into multiple class files using partial keyword with examples

C# Partial Class

In c#, a partial class is useful to split the functionality of particular class into multiple class files and all these files will be combined into one single class file when the application is compiled.

 

While working on large scale projects, multiple developers wants to work on the same class file at the same time. To solve this problem, c# provides an ability to spread the functionality of particular class into multiple class files using partial keyword.

 

In c#, we can use partial keyword to split the definition of particular class, structure, interface or a method over two or more source files.

 

Following is the example of splitting the definition of User class into two class files, User1.cs and User2.cs.

User1.cs

public partial class User

{

    private string name;

    private string location;

    public User(string a, string b)

    {

        this.name = a;

        this.location = b;

    }

}

If you observe above code, we created a partial class called User in User1.cs class file using partial keyword with required variables and constructor.

User2.cs

public partial class User

{

    public void GetUserDetails()

    {

        Console.WriteLine("Name: " + name);

        Console.WriteLine("Location: " + location);

    }

}

If you observe above code, we created a partial class called User in User2.cs class file using partial keyword with GetUserDetails() method.

 

When we execute above code, the compiler will combine these two partial classes into one User class like as shown below.

User

public class User

{

    private string name;

    private string location;

    public User(string a, string b)

    {

        this.name = a;

        this.location = b;

    }

    public void GetUserDetails()

    {

        Console.WriteLine("Name: " + name);

        Console.WriteLine("Location: " + location);

    }

}

This is how the compiler will combine all the partial classes into single class while executing the application in c# programming langauge.

Rules to Implement Partial Class

In c#, we need to follow certain rules to implement partial class in our applications.

 

  • To split the functionality of classstructure, interface or a method over multiple files we need to use partial keyword and all files must be available at compile time to form the final type.
  • The partial modifier can only appear immediately before the keywords classstruct or interface.
  • All parts of partial type definitions must be in the same namespace or assembly.
  • All parts of partial type definitions must have the same accessibility, such as public, private, etc.
  • If any partial part is declared as abstract, sealed or base, then the whole type is considered as abstract or sealed or base based on the defined type.
  • In c#, different parts can have different base types but the final type will inherit all the base types.
  • Nested partial types are allowed in partial type definitions.

C# Partial Class Example

Following is the example of defining a partial classes using partial keyword in c# programming language.

 

using System;

 

namespace Tutlane

{

    public partial class User

    {

        private string name;

        private string location;

        public User(string a, string b)

        {

            this.name = a;

            this.location = b;

        }

    }

    public partial class User

    {

        public void GetUserDetails()

        {

            Console.WriteLine("Name: " + name);

            Console.WriteLine("Location: " + location);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            User u = new User("Suresh Dasari", "Hyderabad");

            u.GetUserDetails();

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example we created a partial class User using partial keyword and we are able access all partial classes as single class to perform required operations.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Partial Class Example Result

 

This is how we can split the functionality of classstructureinterface or a method over two or more source files using partial modifier based on our requirements.