官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 车险| 广水市| 泗洪县| 辽源市| 富川| 汉寿县| 安庆市| 东阳市| 阜新市| 松潘县| 临泉县| 临沧市| 长白| 宜兴市| 汪清县| 七台河市| 祥云县| 同心县| 嘉荫县| 韶山市| 武清区| 武宁县| 福清市| 辽阳市| 陵川县| 铜山县| 阳高县| 出国| 铜川市| 衡水市| 紫云| 墨玉县| 拉萨市| 堆龙德庆县| 宽甸| 赫章县| 龙游县| 锡林浩特市| 海淀区| 类乌齐县| 华池县|