簡體   English   中英

C#中的通用部分方法?

[英]generic partial methods in C#?

部分類和部分方法說明:

部分方法可以是通用的。 約束放在定義的部分方法聲明上,並且可以選擇性地在實現方法上重復。 實現聲明中的參數和類型參數名稱不必與定義中的相同。

視覺上什么是"Constraints are put on the defining partial method declaration, and may optionally be repeated on the implementing one."代碼示例"Constraints are put on the defining partial method declaration, and may optionally be repeated on the implementing one." "Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one." ?

我不知道上面 2 條語句在代碼中的視覺效果如何。

宣言

包括通用約束:

public void Method<TName>(int name) where T : class;

執行

沒有重復的通用約束和不同的參數名稱:

public void Method<TOther>(int other)
{
    // ....
}

實現聲明中的參數和類型參數名稱不必與定義中的相同。

public partial class C {
    public partial void M<T>(int x);
}

public partial class C {
    public partial void M<U>(int y) {
        // implementation goes here...
    }
}

M的兩個聲明具有不同的類型參數名稱( TU ),以及不同的參數名稱( xy )。

約束放在定義的部分方法聲明上,並且可以選擇性地在實現方法上重復。

這是不正確的。 C# 語言規范說:

聲明中對應的類型參數必須具有相同的約束(類型參數名稱的模數差異)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM