English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In C#, the partial keyword can be used to split the implementation of classes, structures, methods, or interfaces across multiple .cs files. When the program is compiled, the compiler merges all implementations from multiple .cs files.
See the employeeprops.cs file and employeemethods.cs file that contain the Employee class below.
public partial class Employee { public int EmpId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } }
public partial class MyPartialClass { public Employee(int Id, string name) { this.EmpId = Id; this.Name = name; } public void DisplayEmployeeInfo() { Console.WriteLine(this.EmpId + " " this.FirstName + " " + this.LastName); } public void Save(int id, string firstName, string lastName) { Console.WriteLine("Saved!"); } }
Above, EmployeeProps.cs contains the properties of the Employee class, while employeememethods.cs contains all the methods of the Employee class. These classes will be compiled into one Employee class.
public class Employee { public int EmpId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public Employee(int Id, string name) { this.EmpId = Id; this.Name = name; } public void DisplayEmployeeInfo() { Console.WriteLine(this.EmpId + " " this.FirstName + " " + this.LastName); } public void Save(int id, string firstName, string lastName) { Console.WriteLine("Saved!"); } }
All part class definitions must be located in the same assembly and namespace.
All parts must have the same accessibility, such as public or private, etc.
If any part is declared as abstract, sealed, or base type, then the entire class declaration is of the same type.
Different parts can have different base types, so the final class will inherit all base types.
The partial modifier can only appear before the keywords class, struct, or interface.
Allows nested partial types.
Partial classes or structures can contain a method that can be split into two separate .cs files of the partial class or structure. One of the .cs files must contain the method signature, and the other files can contain the optional implementation of the partial method. Both the declaration and implementation of the method must have the partial keyword.
public partial class Employee { public int EmpId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } partial void GenerateEmployeeId(); }
public partial class MyPartialClass { partial void GenerateEmployeeId() { this.EmpId = random(); } }
Above, the employeeprops.cs file contains the signature of the DisplayEmployeeInfo method, and the employeemethods.cs file contains its implementation. The compiler will merge all parts into one at compile time.
It requires including the signature of the partial method, but does not need to provide the implementation. If the method is called but not implemented, there will be no compile-time or runtime errors.
Partial methods must use the partial keyword and must return void.
Partial methods can have in or ref parameters without out parameters.
Partial methods are implicitly private methods, so they cannot be virtual methods.
Partial methods can be static methods.
Partial methods can be generic methods.