Tutorialsteacher

关注我们

文章
  • C#
  • C# 面向对象编程
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • 控制反转 (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • 正则表达式
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • C# - 入门
  • C# - 版本历史
  • C# - 第一个程序
  • C# - 关键词
  • C# - 类和对象
  • C# - 命名空间
  • C# - 变量
  • C# - 隐式类型变量
  • C# - 数据类型
  • 数字
  • 字符串
  • DateTime
  • 结构体
  • 枚举
  • StringBuilder
  • 匿名类型
  • 动态类型
  • 可空类型
  • C# - 值类型和引用类型
  • C# - 接口
  • C# - 运算符
  • C# - if else 语句
  • C# - 三元运算符 ?
  • C# - Switch 语句
  • C# - For 循环
  • C# - While 循环
  • C# - Do-while 循环
  • C# - 分部类
  • C# - Static 关键字
  • C# - 数组
  • 多维数组
  • 交错数组
  • C# - 索引器
  • C# - 泛型
  • 泛型约束
  • C# - 集合
  • ArrayList
  • List
  • SortedList
  • Dictionary
  • Hashtable
  • Stack
  • Queue
  • C# - 元组
  • C# - 值元组
  • C# - 内置异常
  • 异常处理
  • throw 关键字
  • 自定义异常
  • C# - 委托
  • Func 委托
  • Action 委托
  • Predicate 委托
  • 匿名方法
  • C# - 事件
  • C# - 协变
  • C# - 扩展方法
  • C# - 流 I/O
  • C# - File 类
  • C# - FileInfo 类
  • C# - 对象初始化器
  • OOP - 概述
  • 面向对象编程
  • 抽象
  • 封装
  • 关联与组合
  • 继承
  • 多态
  • 方法重写
  • 方法隐藏
  • C# - SOLID 原则
  • 单一职责原则
  • 开闭原则
  • 里氏替换原则
  • 接口隔离原则
  • 依赖倒置原则
  • 设计模式
  • 单例模式
  • 抽象工厂模式
  • 工厂方法模式
Entity Framework Extensions - 提升 EF Core 9
  批量插入
  批量删除
  批量更新
  批量合并

C# - 分部类和分部方法

在 C# 中,您可以使用 `partial` 关键字将类、结构、方法或接口的实现拆分到多个 `.cs` 文件中。编译器在编译程序时会将来自多个 `.cs` 文件的所有实现组合起来。

考虑以下包含 `Employee` 类的 `EmployeeProps.cs` 和 `EmployeeMethods.cs` 文件。

EmployeeProps.cs
public partial class Employee
{
    public int EmpId { get; set; }
    public string Name { get; set; }
}
EmployeeMethods.cs
public partial class Employee
{
    //constructor
    public Employee(int id, string name){
        this.EmpId = id;
        this.Name = name;
    }

    public void DisplayEmpInfo() {
        Console.WriteLine(this.EmpId + " " this.Name);
    }
}

上面,`EmployeeProps.cs` 包含 `Employee` 类的属性,而 `EmployeeMethods.cs` 包含 `Employee` 类的所有方法。这些文件将被编译成一个 `Employee` 类。

示例:组合类
public class Employee
{
    public int EmpId { get; set; }
    public string Name { get; set; }

    public Employee(int id, string name){
        this.EmpId = id;
        this.Name = name;
    }

    public void DisplayEmpInfo(){
        Console.WriteLine(this.EmpId + " " this.Name );
    }
}

分部类的规则

  • 所有分部类定义必须在相同的程序集和命名空间中。
  • 所有部分必须具有相同的可访问性,例如 public 或 private 等。
  • 如果任何部分被声明为 abstract、sealed 或基类型,则整个类都被声明为相同的类型。
  • 不同的部分可以有不同的基类型,因此最终类将继承所有基类型。
  • `partial` 修饰符只能紧接在 `class`、`struct` 或 `interface` 关键字之前。
  • 允许嵌套分部类型。

分部方法

分部类或结构可以包含一个方法,该方法被拆分到分部类或结构的两个单独的 `.cs` 文件中。其中一个 `.cs` 文件必须包含该方法的签名,而另一个文件可以包含分部方法的可选实现。方法的声明和实现都必须具有 `partial` 关键字。

EmployeeProps.cs
public partial class Employee
{
    public Employee() { 
        GenerateEmpId();
    }
    public int EmpId { get; set; }
    public string Name { get; set; }

    partial void GenerateEmployeeId();

}
EmployeeMethods.cs
public partial class Employee
{
    partial void GenerateEmployeeId()
    {
        this.EmpId = random();
    }
}

上面,`EmployeeProps.cs` 包含分部方法 `GenerateEmployeeId()` 的声明,该方法在构造函数中使用。`EmployeeMethods.cs` 包含 `GenerateEmployeeId()` 方法的实现。以下代码演示了如何创建一个使用分部方法的 `Employee` 类对象。

EmployeeMethods.cs
class Program
{
    static void Main(string[] args)
    {
        var emp = new Employee();
        Console.WriteLine(emp.EmpId); // prints genereted id

        Console.ReadLine();
    }
}

分部方法的规则

  • 分部方法必须使用 `partial` 关键字,并且必须返回 `void`。
  • 分部方法可以有 `in` 或 `ref` 参数,但不能有 `out` 参数。
  • 分部方法是隐式私有方法,因此不能是虚方法。
  • 分部方法可以是静态方法。
  • 分部方法可以是泛型方法。

在此处了解更多关于分部类和分部方法的信息。

TUTORIALSTEACHER.COM

TutorialsTeacher.com 是您权威的技术教程来源,旨在通过循序渐进的方法,指导您掌握各种网络和其他技术。

我们的内容旨在帮助所有水平的学习者轻松快速地学习技术。通过访问此平台,即表示您已阅读并同意遵守我们的使用条款和隐私政策,这些条款和政策旨在保护您的体验和隐私权。

[email protected]

关于我们使用条款隐私政策
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) 版权所有。