Partial关键字用于将类定义放入同一命名空间中的不同文件中。
partial关键字使类代码的可读性变得更容易。我
们可以把它读入不同的文件。这意味着我们可以在多个文件中物理地分离一个类。
这些类分布在同一命名空间下的几个文件中。
如果程序的长度要增加,那么我们可以将类划分到不同的文件中,以最小化程序的长度并使其易于理解,并且在声明类时必须使用Partial关键字使类成为Partial。
C# Partial类示例
在C#中将类Partial化的代码示例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace parclass
{
class Program
{
static void Main(string[] args)
{
student obj = new student();
obj.show();
obj.display();
Console.ReadLine();
}
}
public partial class student
{
public void show()
{
Console.WriteLine("Welcome");
}
}
}
现在,添加类文件要执行此操作,请按以下步骤在不同名称空间下的单独文件中进行类定义:
- 转到解决方案资源管理器。
- 右键单击它并选择"添加"选项。
- 然后选择新项目并选择类模板。
- 然后更改类的名称。
- 然后单击"添加"按钮。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace parclass
{
public partial class student
{
public void display()
{
Console.WriteLine("hi");
}
}
}
在上面的程序中我们可以看到,在同一个名称空间类parclass下,我们制作了一个类student的两个单独的文件,并且可以组合访问两个文件的函数。
日期:2020-04-11 23:03:47 来源:oir作者:oir
