当前位置:网站首页>C# - partial 关键字
C# - partial 关键字
2022-07-24 15:20:00 【wumingxiaoyao】
C# partial 关键字
引言
partial 关键字用于拆分一个类、一个结构、一个接口或一个方法的定义到两个或更多的文件中。 每个源文件包含类型或方法定义的一部分,编译应用程序时将把所有部分组合起来。在设计 Framework 时,可以充分利用 partial 这个特性。
分部类
什么情况下需要拆分类定义呢?
- 处理大型项目时,使一个类分布于多个独立文件中可以让多位程序员同时对该类进行处理。
- 当使用自动生成的源文件时,你可以添加代码而不需要重新创建源文件。 Visual Studio 在创建 Windows 窗体、Web 服务包装器代码等时会使用这种方法。 你可以创建使用这些类的代码,这样就不需要修改由 Visual Studio 生成的文件。
- 使用源生成器在类中生成附加功能时。
例子
将 Coords 类 分部在 2 个文件中定义。
CoordsOne.cs
定义了 Coords 类的构造函数
注意签名 partial class Coords
namespace ConsoleApp1.PartialClass
{
public partial class Coords
{
private int x;
private int y;
public Coords(int x, int y)
{
this.x = x;
this.y = y;
}
}
}
CoordsTwo.cs
定义了 Coords 类的一个方法
注意签名 partial class Coords
namespace ConsoleApp1.PartialClass
{
public partial class Coords
{
public void PrintCoords()
{
Console.WriteLine("Coords: {0},{1}", x, y);
}
}
}
TestPartial.cs
测试一下上面的分部类
using ConsoleApp1.PartialClass;
namespace ConsoleApp1
{
internal class TestPartial
{
static void Main(string[] args)
{
Coords coords = new Coords(10, 20);
coords.PrintCoords();
}
}
}
结果:
Coords: 10,20
partial 分部限制
处理分部类定义时需遵循下面的几个规则:
- 要作为同一类型的各个部分的所有分部类型定义都必须使用
partial进行修饰。
例如,下面的类声明会生成错误:
public partial class A {
}
//public class A { } // Error, must also be marked partial
partial修饰符只能出现在紧靠关键字class、struct或interface前面的位置。分部类型定义中允许使用嵌套的分部类型,如下面的示例中所示:
partial class ClassWithNestedClass
{
partial class NestedClass {
}
}
partial class ClassWithNestedClass
{
partial class NestedClass {
}
}
要成为同一类型的各个部分的所有分部类型定义都必须在同一程序集和同一模块(.exe 或 .dll 文件)中进行定义。 分部定义不能跨越多个模块。
经测试这些分部的文件必须在同一命名空间。
还是上面的例子,CoordsThree.cs 也定义成partial类,但是相比 CoordsOne.cs 和 CoordsTwo.cs 不是在同一个命名空间,会出错。

类名和泛型类型参数在所有的分部类型定义中都必须匹配。 泛型类型可以是分部的。 每个分部声明都必须以相同的顺序使用相同的参数名。
如果某关键字出现在一个分部类型定义中,则该关键字不能与在同一类型的其他分部定义中指定的关键字冲突:public,private,protected,internal,abstract,sealed 等
将 CoordsOne.cs 中 public 改成 internal,产生访问属性冲突。
分部接口和结构
同理,也可以开发分部结构和接口
partial interface ITest
{
void Interface_Test();
}
partial interface ITest
{
void Interface_Test2();
}
partial struct S1
{
void Struct_Test() {
}
}
partial struct S1
{
void Struct_Test2() {
}
}
分部方法
分部方法在分部类型的一部分中定义了签名,并在该类型的另一部分中定义了实现。 通过分部方法,类设计器可提供与事件处理程序类似的方法挂钩,以便开发者决定是否实现。 如果开发者不提供实现,则编译器在编译时删除签名。 以下条件适用于分部方法:
声明必须以上下文关键字
partial开头。分部类型各部分中的签名必须匹配。
构造函数、终结器、重载运算符、属性声明或事件声明中不允许使用
partial关键字。
例如:
namespace PM
{
partial class A
{
partial void OnSomethingHappened(string s);
}
// This part can be in a separate file.
partial class A
{
// Comment out this method and the program
// will still compile.
partial void OnSomethingHappened(String s)
{
Console.WriteLine("Something happened: {0}", s);
}
}
}
在以下情况下,不需要使用分部方法即可实现:
- 没有任何可访问性修饰符(包括默认的 专用)。
- 返回 void。
- 没有任何输出参数。
- 没有以下任何修饰符:virtual、override、sealed、new 或 extern。
this 和 partial 的区别
C# - this 的用法 一文中有介绍 this 具有扩展类方法的功能,那么 this 和 partial 有区别是?
- 概念的区别,
this是对原有功能进行扩展,partial是将整体分成多个部分存放,便于维护。 - 实现方式的区别
patial分部的签名要求一致,必须是partialclass/interface/struct Name,文件名不一样。
this 扩展类名不一样,但必须是静态的类,静态的方法,方法第一个参数必须是thisClassName - 调用的区别
partial分部的各个文件必须在同一 namespace 命名空间this扩展可以在不同命名空间,但是需要导入扩展的 namespace 命名空间后才能调用扩展的方法。
边栏推荐
- 2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 1: don't waste gold (finished)
- How vscode debug nodejs
- Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出
- C# SQLite Database Locked exception
- JMeter - call the interface for uploading files or pictures
- [machine learning basics] - another perspective to explain SVM
- String application - calculate the longest true prefix of a string
- Calculate the M-day moving average price of two stocks
- Self join usage of SQL
- Discussion on the basic use and address of pointer in array object
猜你喜欢

Android SQLite database practice

VAE(变分自编码器)的一些难点分析

Leetcode high frequency question 56. merge intervals, merge overlapping intervals into one interval, including all intervals
![[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes](/img/4d/b0ba599a732d1390c5eeb1aea6e83c.png)
[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes

Sword finger offer II 001. integer division

Simple encapsulation of wechat applet wx.request

27.目录与文件系统

24.原生磁盘的使用

(09) flask is OK if it has hands - cookies and sessions

How to set packet capturing mobile terminal
随机推荐
Which securities company is good at opening an account with flush? Excuse me, is it safe to open an account with mobile phone or stock?
Strongly connected component
Is it safe for Huatai Securities to open a mobile account and will it be leaked
Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程
JS native - array sorting to find out the characters with the largest number of strings
Summary of feature selection: filtered, wrapped, embedded
C# SQLite Database Locked exception
26. Code implementation of file using disk
The first n rows sorted after dataframe grouping nlargest argmax idmax tail!!!!
Property datasource is required exception handling [idea]
Which securities company is the best and safest to open an account? How to open an account and speculate in stocks
Various searches (⊙▽⊙) consolidate the chapter of promotion
spark学习笔记(三)——sparkcore基础知识
Android SQLite database practice
图的存储和遍历
JMeter - call the interface for uploading files or pictures
(09) flask is OK if it has hands - cookies and sessions
AG. DS binary tree -- hierarchical traversal
在哪家证券公司开户最好最安全 如何开户炒股票
Outlook tutorial, how to set rules in outlook?