[英]How can i create a c# method that will return the same collection type as as argument with a different element type?
本質上,與該問題完全相同,但在c#中。
我想要這樣的東西:
public IList<double> DoSomething(IList<string> strings) {
//something
}
但是我寧願鍵入它,以便提前知道返回值,這樣就不必強制轉換或在函數外進行任何操作。 我該怎么做呢?
有沒有通用的方法
public ListType<double> DoSomething<ListType>(ListType<string> strings) where ListType : ???{
設置嗎?
C#不允許完全滿足您的要求,但是最接近的是:
public TList2 DoSomething<TList1, TList2>(TList1 strings)
where TList1 : IEnumerable<string>
where TList2 : ICollection<decimal>, new()
{
var result = new TList2();
foreach (var s in strings)
{
result.Add(decimal.Parse(s));
}
return result;
}
和一個例子:
var input = new HashSet<string>(new[] { "1", "2", "3"});
List<decimal> output = DoSomething<HashSet<string>, List<decimal>>(input);
您可以使用Linq執行此操作。
例如,如果您只想解析為兩倍,則可以執行以下操作:
List<double> result = strings.Select(double.Parse).ToList()
您可以發送任何其他方法來代替double.Parse:
List<double> result = strings.Select(DoSomethingWithOneItem).ToList()
double DoSomethingWithOneItem(string item) {
//your conversion logic
}
不幸的是,例如IList<TInput>
和IList<TOutput>
之間沒有任何可用於幫助的關系,因此您將需要指定輸入和輸出列表類型,這在常規形式中會變得有些麻煩:
IList<string> strings = new List<string> {"1.1", "2.2", "3.3"};
IList<decimal> result = strings.ConvertToSameListType((Func<string, decimal>)decimal.Parse, () => new List<decimal>());
public static class EnumerableExtensioncGeneralVersion
{
public static TOutputList ConvertToSameListType<TInputList, TOutputList, TInput, TOutput>(this TInputList source, Func<TInput, TOutput> itemConversion, Func<TOutputList> outputListConstructor)
where TInputList : IEnumerable<TInput>
where TOutputList : ICollection<TOutput>
{
TOutputList result = outputListConstructor();
foreach (TOutput convertedItem in source.Select(itemConversion))
{
result.Add(convertedItem);
}
return result;
}
}
盡管如果只為要支持的每種喜歡的收藏集類型指定一種擴展方法,則可以使使用轉換更好用:
//Seting up inputs
IList<string> strings = new List<string> {"1.1", "2.2", "3.3"};
IEnumerable<string> enumerableStrings = strings.Select(x => x);
ObservableCollection<string> observableStrings = new ObservableCollection<string>(strings);
//Converting
IList<decimal> resultList = strings.Convert(decimal.Parse);
IEnumerable<decimal> resultEnumerable = enumerableStrings.Convert(decimal.Parse);
ObservableCollection<decimal> observableResult = observableStrings.Convert(decimal.Parse);
public static class EnumerableExtensions
{
public static IList<TOutput> Convert<TInput, TOutput>(this IList<TInput> source, Func<TInput, TOutput> itemConversion)
{
return source.Select(itemConversion).ToList();
}
public static IEnumerable<TOutput> Convert<TInput, TOutput>(this IEnumerable<TInput> source, Func<TInput, TOutput> itemConversion)
{
return source.Select(itemConversion);
}
public static ObservableCollection<TOutput> Convert<TInput, TOutput>(this ObservableCollection<TInput> source, Func<TInput, TOutput> itemConversion)
{
return new ObservableCollection<TOutput>(source.Select(itemConversion));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.