可能重複:
How can I create a singleton IEnumerable?
Favorite way to create an new IEnumerable<T> sequence from a single value?我怎麼能枚舉一個項目
說我需要返回只包含一個元素一個IEnumerable。
我會返回一個列表中
return new List<Whatever> (myItem);
或與此有關的數組。
但我在我的代碼把這個處處之前最好將創建一個Singleton方法
public IEnumerable<T> Singleton (T t)
{
yield return t
}
,是不是有這已經這樣做的方法?
爲什麼你覺得你需要枚舉一個單身人士? –
爲什麼單身人士更可取?這似乎是很少需要的東西,而且應該是專門針對該流程設計的。 –
相關:http://stackoverflow.com/questions/1019737/favorite-way-to-create-an-new-ienumerablet-sequence-from-a-single-value –