Splitting classes using partial
When working on large projects with multiple team members, it is useful to be able to split the definition of a complex class across multiple files. You do this using the partial
keyword.
Imagine we want to add a new method to the Person
class without having to ask another programmer to close the Person.cs
file. If the class is defined as partial
, then we can split it over as many separate files as we like.
In the Person
class, add the partial
keyword, as shown highlighted in the following code:
namespace Packt.CS7
{
public partial class Person
{
In Visual Studio 2017, on the Project menu, go to Add Class... or press Shift + Alt +
C. Enter the name Person2
. We cannot enter Person
because Visual Studio 2017 isn't ...
Get C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.