CLR Via CSharp读书笔记(8):方法

转换操作符:

public sealed class Rational {
    public Rational(Int32 num) { /* ... */ }
    public Int32 ToInt32() { /* ... */ return 0; }

    // Implicitly constructs and returns a Rational from an Int32
    public static implicit operator Rational(Int32 num) {
        return new Rational(num);
    }

    // Explicitly returns an Int32 from a Rational
    public static explicit operator Int32(Rational r) {
        return r.ToInt32();
    }
}

分部方法

分部方法只能在分部类或结构中声明。

返回类型始终是void,参数不能用out修饰符来标记,可以用ref来标记,也不能再代码中创建一个委托来引用这个分部方法。根本原因在于分部方法在运行时可能不存在,如果没有实现分部方法的话。

分部方法总是被视为private方法,但是,C#编译器禁止在分部方法声明之前添加private关键字。

原文地址:https://www.cnblogs.com/thlzhf/p/2805430.html