- Hands-On Object:Oriented Programming with C#
- Raihan Taher
- 271字
- 2021-07-02 12:44:43
The partial class
You can split a class, a struct, or an interface into smaller portions that can be placed in different code files. If you want to do this, you have to use the keyword partial. Even though the code is in separate code files, when complied, they will be treated as one class altogether. There are many benefits of partial classes. One benefit is that different developers can work on different code files at a time. Another benefit is that if you are using autogenerated code and you want to extend some functionality of that autogenerated code, you can use a partial class in a separate file. Consequently, you are not directly touching the autogenerated code, but adding new functionality in the class.
The partial class has a few requirements, one of which is that all classes must have the keyword partial in their signatures. All the partial classes also have to have the same name, but the file names can be different. The partial classes also have to have the same accessibility, such as public, private, and so on.
The following is an example of a partial class:
// File name: Animal.cs
using System;
namespace AnimalProject {
public partial class Animal {
public string name;
public int ageInMonths;
public void Eat(){
Console.WriteLine("Eating");
}
}
}
// File name: AnimalMoving.cs
using System;
namespace AnimalProject {
public partial class Animal {
public void Move(){
Console.WriteLine("Moving");
}
}
}
As shown in the preceding code, you can create many partial classes of a class. This will increase the readability of your code, and your code organization will be more structured.
- The DevOps 2.3 Toolkit
- WebAssembly實戰
- Python爬蟲開發:從入門到實戰(微課版)
- JavaScript 網頁編程從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- 從程序員到架構師:大數據量、緩存、高并發、微服務、多團隊協同等核心場景實戰
- GitLab Repository Management
- ANSYS Fluent 二次開發指南
- Java程序設計入門
- Access 2010數據庫應用技術(第2版)
- Python圖形化編程(微課版)
- Java高并發核心編程(卷1):NIO、Netty、Redis、ZooKeeper
- 軟件測試教程
- AMP:Building Accelerated Mobile Pages
- Learning Zimbra Server Essentials
- Swift編程實戰:iOS應用開發實例及完整解決方案