首页  >  文章  >  后端开发  >  C# 中的部分

C# 中的部分

王林
王林原创
2024-09-03 15:31:23322浏览

C# 的一个特殊功能是分部类,使用分部类可以将单个类功能实现到多个文件中,然后在应用程序编译期间将这些文件组合在一起形成单个类文件,并且使用partial 关键字在 C# 中创建分部类,此分部关键字还用于将方法功能、接口功能或结构功能划分为多个文件,并且所有这些文件在强制编译期间必须可用,以创建最终文件和用户还可以使用嵌套的部分类型。

语法:

public partial Class_name
{
// code
}

C# 中的部分类的函数

必须在任何类名之前使用partial关键字才能使该类成为分部类。可以使用partial关键字将接口、结构或类分为多个部分。通过将类作为分部类,可以将单个类分为多个文件。在部分代码的编译过程中,多个类或多个接口或结构被编译为单个类或单个接口或结构。可以使用partial关键字将用于设计的用户界面代码与用于业务逻辑的代码分开,这使得工作和理解更容易。

使用分部类,多个开发人员可以并行工作。定制的逻辑代码可以通过使用部分类嵌入到框架自动生成的代码中。通过将较大的类划分为较小的类,可以轻松地理解和维护较大的类。通过将接口划分为多个可以与多个开发人员共享的代码,可以加快应用程序的开发速度。密封部分类会导致整个类被密封。这称为分部类的密封属性。将部分类抽象化会导致整个类成为抽象类。这称为分部类的抽象属性。具有相同名称的分部类必须仅在同一命名空间范围内声明。

在 C# 中实现 Partial 的示例

考虑以下示例来理解 C# 中分部类的概念:

示例#1

 代码:

using System
public class Check
{
//main method is called
public static void Main()
{
//the same partuial class is defined at two places twice but during compilation it is executed as a single file
parclass pc=new parclass();
pc.firstmethod();
pc.secmethod();
}
//partial class is defined with the same class name
public partial class parclass
{
//a method is declared
public void firstmethod()
{
Console.WriteLine("The first method is called");
}
}
//another partial class is defined with the same name
public partial class parclass
{
//another method is declared
public void secmethod()
{
Console.WriteLine("The second method is called");
}
}
}

输出:

C# 中的部分

说明:在上面的程序中,定义了一个名为 check 的类,在该类中调用了 main 方法。该主方法由稍后定义的分部类的实例组成,使用该实例来调用分部类的方法。定义了两个具有相同名称 parclass 的分部类。它们包含在主方法中调用的不同方法。分部类在编译过程中将多个类合并为一个类,输出如上面的快照所示。

示例#2

C# 程序演示分部类,同时为变量赋值并使用两个不同的类打印它们。

 代码:

using System;
public class Check
{
//main method is called
public static void Main()
{
//the same partial class is defined at two places twice but during compilation it is executed as a single file
rec r=new rec(5,10);
r.print();
Console.ReadLine();
}
//partial class is defined with the same class name
public partial class rec
{
private int a;
private int b;
//a method is declared
public rec(int a, int b)
{
this.a = a;
this.b = b;
}
}
//another partial class is defined with the same name
public partial class rec
{
//another method is declared
public void print()
{
Console.WriteLine("The value of a is "+ a);
Console.WriteLine("The value of b is "+ b);
}
}
}

输出:

C# 中的部分

说明:在上面的程序中,定义了一个名为 check 的类,在该类中调用了 main 方法。该主方法由稍后定义的分部类的实例组成,使用该实例来调用分部类的方法。定义了两个同名的分部类。它们包含在主方法中调用的不同方法。分部类在编译过程中将多个类合并为一个类,输出如上面的快照所示。

示例 #3

C# 程序演示分部类,同时为变量赋值并使用两个不同的类打印它们。

代码:

using System;
public class Check
{
//main method is called
public static void Main()
{
//the same partial class is defined at two places twice but during      compilation it is executed as a single file
stat r=new stat();
r.print1();
r.print2();
}
//partial class is defined with the same class name
public partial class stat
{
public void print1()
{
Console.WriteLine("Hello, welcome to Partial class one");
}
}
//another partial class is defined with the same name
public partial class stat
{
//another method is declared
public void print2()
{
Console.WriteLine("Hello, welcome to partial class two");
}
}
} <strong>Output:</strong>

C# 中的部分

说明:在上面的程序中,定义了一个名为 check 的类,在该类中调用了 main 方法。该主方法由稍后定义的分部类的实例组成,使用该实例来调用分部类的方法。定义了两个具有相同名称 stat 的分部类。它们包含在主方法中调用的不同方法。分部类在编译过程中将多个类合并为一个类,输出如上面的快照所示。

以上是C# 中的部分的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# Create JSON Object下一篇:C# readonly