Skip to content
Rain Hu's Workspace
Go back

[C#] Partial Type 局部類型

Rain Hu

1. 用法

public partial class Util
{
    public static IEnumerable<int> GetFibonacci() { ... }
}

public partial class Util
{
    public static IEnumerable<int> GetPrime() { ... }
}

2. Partial Method 局部方法

partial class Demo
{
    public Demo()
    {
        OnConstruction();                               // 調用未實現的局部方法
    }
    public override string ToString()
    {
        string ret = "Original return value";
        CustomizeToString(ref ret);
        return ret;
    }
    partial void OnConstruction();                      // 宣告局部方法
    partial void CustomizeToString(ref string text);    // 宣告局部方法
}

partial class Demo
{
    partial void CustomizeToString(ref string text)     // 實作局部方法
    {
        text += " - Customized!";
    }
    
    public static void Main(string[] args)
    {
        Demo d = new Demo();
        Console.WriteLine(d.ToString());
    }
}

Share this post on:

Previous
[C#] C# 筆記
Next
[C#] IEnumerable & IEnumerator 迭代器