簡體   English   中英

如何在 C# 中的 function 中使用多個泛型

[英]how to use more than one generic in a function in C#

我想為 function 使用多個泛型,但這不起作用

  protected async Task<Tlist,T> Send<Tlist,T>(string url,HttpMethod method,DateTimeSyncedAt ) where Tlist,T:class
    {
    }

我收到這些錯誤

任務不支持返回多種類型作為返回值。

所以你基本上可以像這樣使用它

Task<T>
//The following wont work
Task<T,T1>

但是您可以使用元組或任何value to value

價值對價值對

元組:

元組是具有特定數量和值序列的數據結構。 Tuple<T1,T2> class 表示一個 2 元組或對,它是一個元組 元組通常由其運算符 (T, T1) 使用 元組中的類型可能超過兩個,這取決於用例。

詞典:

C# 中的字典是 System.Collection.Generics 命名空間中包含鍵和值的通用集合類型。

字典可以通過以下方式初始化:

    new Dictionary<string, string>(new []{new KeyValuePair<string, string>("SOMETHING1","SOMETHING2")});

如果將此字典包裝到我們稱為dictionary的變量中,則可以通過以下方式分別訪問鍵和值

dictionary.Keys // this is an array of keys in the dictionary
dictionary.Values // this is an array of values in the dictionary

執行以下操作:

    protected async Task<(TList, T)> Send<TList,T>(string aParam,string bParam, string cParam) where TList : IEnumerable<T> where T : struct
    {
    }

在這種情況下,這將返回一個具有指定類型的元組(item1: LIST_OF_GUID, GUID)

但是您可以將其與任何類型的約束一起使用

暫無
暫無

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

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