What is a Partial Class in .NET?

A partial class is a class that can be split across many files. This makes it ideal for when multiple users want to work on the same class but not have to wait for the file to be freed up by another developer. You have two files each with a partial class in it and carry on working.

Functions in each partial class must not overlap one another, you can have FullName() in each class unless they are polymorphic.

The only limitation to this is that the partial classes must be in the same namespace and solution for this to work.

public partial class Person
{
public string FirstName { get; set; }
public string FullName() { return FirstName + " " + SurName; }
}

public partial class Person
{
public string SurName { get; set; }
}

class Program
{
static void Main(string[] args)
{
    Person A = new Person();
    A.FirstName = "Joe";
    A.SurName = "Bloggs";
    Console.WriteLine(“Employee :- ” + A.FullName().ToString());
    Console.ReadLine();
}
}


Tags - .NET   Class

Last Modified : June 2023


About... / Contact.. / Cookie...