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 smart enough to understand what we want to do. Instead, we must now rename the new class to Person, change the namespace, and add the public partial keywords, as shown in the following code:

    namespace Packt.CS7 
    { 
      public partial class Person 
      { 

In Visual Studio Code, click the New File button in the Ch06_PacktLibrary folder in the Explorer pane and enter a name of Person2.cs. Add the following statements to the new file:

    namespace Packt.CS7 
    { 
      public partial class Person 
      { 
      } 
    } 

Note

The rest of the code we write for this chapter will be written in the Person2.cs file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.47