传递为IEnumerable℃的单一项目; T> [英] Passing a single item as IEnumerable<T>

查看:224
本文介绍了传递为IEnumerable℃的单一项目; T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有通过类型的 T单个项目来它期望方法的的IEnumerable℃的常见方式; T> 参数?语言是C#,框架2.0版本。

目前我使用一个辅助方法(它的.Net 2.0,所以我有一大堆的铸件/投影辅助方法类似于LINQ),但这似乎只是愚蠢的:

 公共静态类IEnumerableExt
{
    //用法:IEnumerableExt.FromSingleItem(someObject);
    公共静态的IEnumerable< T> FromSingleItem< T>(T项)
    {
        产生收益的项目;
    }
}
 

当然,

另一种方法是创建并填充名单,其中,T> 阵列,并通过它而不是的IEnumerable< T>

作为一个扩展方法,它可能被命名为:

 公共静态类IEnumerableExt
{
    //用法:someObject.SingleItemAsEnumerable();
    公共静态的IEnumerable< T> SingleItemAsEnumerable< T>(这件T项)
    {
        产生收益的项目;
    }
}
 

我失去了一些东西呢?

我们发现 someObject.Yield()(如@Peter在下面的意见建议)是这样做的最好的名字扩展方法,主要是为了简洁起见,所以这里是一个随着XML注释如果有人想抓住它:

 公共静态类IEnumerableExt
{
    ///<总结>
    ///此对象实例包装了到一个IEnumerable和放大器; LT; T&放大器; GT;
    ///由单个项目。
    ///< /总结>
    ///< typeparam名=T>对象的类型。 < / typeparam>
    ///< PARAM NAME =项>将被包裹的实例。 < /参数>
    ///<返回>一个IEnumerable和放大器; LT; T&放大器; GT;由单个项目。 < /回报>
    公共静态的IEnumerable< T>产量< T>(这件T项)
    {
        产生收益的项目;
    }
}
 

解决方案

您的helper方法是做它,IMO最彻底的方法。如果你传递一个列表或一个数组,那么一个不择手段的一块code可能转换,改变的内容,导致在某些情况下,古怪的行为。你可以使用一个只读集合,但是这可能会涉及更多的包装。我觉得,因为它得到您的解决方案是为整齐。

Is there a common way to pass a single item of type T to a method which expects an IEnumerable<T> parameter? Language is C#, framework version 2.0.

Currently I am using a helper method (it's .Net 2.0, so I have a whole bunch of casting/projecting helper methods similar to LINQ), but this just seems silly:

public static class IEnumerableExt
{
    // usage: IEnumerableExt.FromSingleItem(someObject);
    public static IEnumerable<T> FromSingleItem<T>(T item)
    {
        yield return item; 
    }
}

Other way would of course be to create and populate a List<T> or an Array and pass it instead of IEnumerable<T>.

[Edit] As an extension method it might be named:

public static class IEnumerableExt
{
    // usage: someObject.SingleItemAsEnumerable();
    public static IEnumerable<T> SingleItemAsEnumerable<T>(this T item)
    {
        yield return item; 
    }
}

Am I missing something here?

[Edit2] We found someObject.Yield() (as @Peter suggested in the comments below) to be the best name for this extension method, mainly for brevity, so here it is along with the XML comment if anyone wants to grab it:

public static class IEnumerableExt
{
    /// <summary>
    /// Wraps this object instance into an IEnumerable&lt;T&gt;
    /// consisting of a single item.
    /// </summary>
    /// <typeparam name="T"> Type of the object. </typeparam>
    /// <param name="item"> The instance that will be wrapped. </param>
    /// <returns> An IEnumerable&lt;T&gt; consisting of a single item. </returns>
    public static IEnumerable<T> Yield<T>(this T item)
    {
        yield return item;
    }
}

解决方案

Your helper method is the cleanest way to do it, IMO. If you pass in a list or an array, then an unscrupulous piece of code could cast it and change the contents, leading to odd behaviour in some situations. You could use a read-only collection, but that's likely to involve even more wrapping. I think your solution is as neat as it gets.

这篇关于传递为IEnumerable℃的单一项目; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆