簡體   English   中英

IEnumerable <T,int>,Arity和Generic Type Definitions

[英]IEnumerable<T, int>, Arity and Generic Type Definitions

我有一個班級計數器 ,用鑰匙計算東西。 簡化:

public class Counter<T> {
    private Dictionary<T, int> counts;

    public void Increment(T key) {
        int current;
        bool exists = counts.TryGetValue(key, out current);
        if (exists) {
            counts[key]++;
        } else {
            counts[key] = 1;
        }
    }
}

它還有許多其他專門針對我需求的東西,但這才是最重要的。 到目前為止,它運作良好。

現在我想讓它在Linq查詢中使用(包含鍵和值)。 做到這一點,我想我需要實施

IEnumerable<T, int>

所以我補充說:

public class Counter<T> : IEnumerable<KeyValuePair<T, int>> {
    // ...
    IEnumerator<KeyValuePair<T, int>> 
    IEnumerable<KeyValuePair<T, int>>.GetEnumerator()
    {
        return ((IEnumerable<KeyValuePair<T, int>>)counts).GetEnumerator();
    }
    System.Collections.IEnumerator 
    System.Collections.IEnumerable.GetEnumerator()
    {
        return counts.GetEnumerator();
    }

遺憾的是,這會導致編譯器錯誤

提供的泛型參數的數量不等於泛型類型定義的arity。 參數名稱:實例化

問題

  1. 什么是arity?
  2. 我是否正確地從Linq使用此類型?
  3. 如何修復實施?

更新 :錯字

我有一個拼寫錯誤,同時簡化我的代碼發布。 代碼實際上是在嘗試實現IEnumerable<KeyValuePair<T, int>>而不是IEnumerable<T, int>

  1. Arity是一種說“參數數量”的奇特方式。 這是單詞“二進制”(取兩個參數),“一元”(取一個參數)和“三元”(取三個參數)的根。
  2. 不,不完全:LINQ植根於函數式編程,函數編程討厭所有狀態,更喜歡沒有副作用的函數。 不幸的是,你的計數器保持狀態:這是你修改的counts字典,這是副作用。
  3. 如果您想按鍵計數,LINQ已經為您提供了足夠的設施。

以下是按鍵獲取項目計數的方法:

var counters = keyedData
    .GroupBy(item => item.MyKey)
    .ToDictionary(g => g.Key, g => g.Count());

無法隱式轉換類型 'System.Collections.Generic.List<object> ' 到 'System.Collections.Generic.IEnumerable<int><div id="text_translate"><p> 我想構建一個 function,它接受一個非負整數和字符串的列表,並返回一個過濾掉字符串的新列表。</p><p> 與此類似:</p><pre> ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b"}) => {1, 2} ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", 0, 15}) => {1, 2, 0, 15}</pre><p> 為此,我執行了以下操作:(評論顯示了最初的嘗試)</p><pre> using System.Collections; using System.Collections.Generic; using System.Linq; public class ListFilterer { public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems) { foreach (var item in listOfItems) { if(item is string) { listOfItems.Remove(item); } } // return listOfItems; <- Original attempt return listOfItems.ToList(); } }</pre><p> 這給出了標題中的錯誤:</p><pre> src/Solution.cs(16,13): error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int>'. An explicit conversion exists (are you missing a cast?)</pre><p> 所以我添加了我認為是正確的轉換.ToList()但它最終沒有做任何事情並且仍然提供相同的錯誤。 我對此感到困惑,因為在搜索和查看我認為類似的問題后,我仍然沒有找到正確的方法來轉換它,並且由於我對 Linq 和 Enumerable 的經驗不足,我不確定去哪里找。</p><p> 任何幫助將不勝感激,謝謝你的時間</p></div></int></object>

[英]Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int>

暫無
暫無

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

相關問題 用於匹配IEnumerable <T>的任何內容的通用類型參數 比較泛型:“泛型” IEnumerable &lt;&gt;,是泛型的,但與所有特定類型匹配(IEnumerable <int> ,IEnumerable <object> ,…)? IEnumerable類型的參數 <IEnumerable<string> &gt;接受清單 <List<string> &gt;而IEnumerable &lt;(int,IEnumerable <string> )&gt;不是嗎? 在Ninject中綁定 - 提供的泛型參數不等於泛型類型定義的arity? 不能處理IEnumerable <int?> 一般而言 如何創建采用T或IEnumerable的通用類型 <T> ? 類型的通用方法 <T> 在運行時並將結果存儲在IEnumerable中 <T> 如果T是IEnumerable而無需強制轉換,則遍歷泛型T 返回一個通用的IEnumerable <T> 無法隱式轉換類型 'System.Collections.Generic.List<object> ' 到 'System.Collections.Generic.IEnumerable<int><div id="text_translate"><p> 我想構建一個 function,它接受一個非負整數和字符串的列表,並返回一個過濾掉字符串的新列表。</p><p> 與此類似:</p><pre> ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b"}) => {1, 2} ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", 0, 15}) => {1, 2, 0, 15}</pre><p> 為此,我執行了以下操作:(評論顯示了最初的嘗試)</p><pre> using System.Collections; using System.Collections.Generic; using System.Linq; public class ListFilterer { public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems) { foreach (var item in listOfItems) { if(item is string) { listOfItems.Remove(item); } } // return listOfItems; <- Original attempt return listOfItems.ToList(); } }</pre><p> 這給出了標題中的錯誤:</p><pre> src/Solution.cs(16,13): error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int>'. An explicit conversion exists (are you missing a cast?)</pre><p> 所以我添加了我認為是正確的轉換.ToList()但它最終沒有做任何事情並且仍然提供相同的錯誤。 我對此感到困惑,因為在搜索和查看我認為類似的問題后,我仍然沒有找到正確的方法來轉換它,並且由於我對 Linq 和 Enumerable 的經驗不足,我不確定去哪里找。</p><p> 任何幫助將不勝感激,謝謝你的時間</p></div></int></object>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM