Partial Classes and Methods
Learn to split a single class across multiple files.
We'll cover the following...
Partial classes
There is a C# feature that allows us to split class definition into multiple locations or files. Using the partial
keyword, we inform the compiler that the class source code is located in multiple places:
Press + to interact
Program.cs
PersonMethods.cs
Person.cs
using System;namespace PartialClasses{class Program{static void Main(string[] args){// Despite the class definition is contained in separate files,// it is one classvar person = new Person(){FirstName = "John",LastName = "Doe",Age = 34,Occupation = "Software Developer",AnnualSalary = 120000m};person.PrintFullName();}}}
Partial classes are a C# compiler feature. Partial class definitions are assembled together before they’re compiled to IL code:
public partial class C
{
public void M()
{
}
}
public partial class C
{
public void K()
{
}
}
The code above compiles ...