English 中文(简体)
如何计算一个项目[复制]
原标题:How can I enumerable one item [duplicate]
This question already has answers here:
Closed 12 years ago.

Possible Duplicates:
How can I create a singleton IEnumerable?
Favorite way to create an new IEnumerable<T> sequence from a single value?

我需要回来一份只包含一个内容的电子数字。

我可以回到名单上。

return new List<Whatever> (myItem);

或为此而设一个阵列。

But it would be preferable to create a Singleton method

public IEnumerable<T> Singleton (T t)
{
yield return t
}

在我把这处地方都列入法典之前,是否有办法这样做?

问题回答

现有最接近的方法。 NET框架是Evidable.Repeat(myItem,1),但我只使用new[]{ IItem}

Enumerable.Repeat(t, 1);

这似乎相当。 不知道什么。

/// <summary>
/// Retrieves the item as the only item in an IEnumerable.
/// </summary>
/// <param name="this">The item.</param>
/// <returns>An IEnumerable containing only the item.</returns>
public static IEnumerable<TItem> AsEnumerable<TItem>(this TItem @this)
{
    return new [] { @this };
}

Taken from Passing a single item as IEnumerable<T>

您能顺便通过单一项目。

new T[] { item } 

页: 1 System.Linq.E amountable 班级

System.Linq.Enumerable.Repeat(item, 1); 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签