Joa*_*nge 54 .net c# ienumerable
基本上我想尝试做这样的事情:
image.Layers
Run Code Online (Sandbox Code Playgroud)
除了图层之外的所有图层都返回IEnumerable Parent
,但在某些情况下,我只想这样做:
image.Layers.With(image.ParentLayer);
Run Code Online (Sandbox Code Playgroud)
因为它只在少数几个地方使用,相比之下,通常使用的100s image.Layers
.这就是为什么我不想创建另一个也返回Parent
图层的属性.
Ani*_*Ani 52
一种方法是从项目(例如数组)中创建单个序列,然后将Concat
其创建到原始序列上:
image.Layers.Concat(new[] { image.ParentLayer } )
Run Code Online (Sandbox Code Playgroud)
如果您经常这样做,请考虑编写一个Append
(或类似的)扩展方法,例如此处列出的方法,它可以让您:
image.Layers.Append(image.ParentLayer)
Run Code Online (Sandbox Code Playgroud)
rea*_*art 26
已经给出了许多实现.我看起来有点不同(但表现同样好)
此外,我发现它也可以控制ORDER.因此,我经常使用ConcatTo方法,将新元素放在前面.
public static class Utility
{
/// <summary>
/// Adds the specified element at the end of the IEnummerable.
/// </summary>
/// <typeparam name="T">The type of elements the IEnumerable contans.</typeparam>
/// <param name="target">The target.</param>
/// <param name="item">The item to be concatenated.</param>
/// <returns>An IEnumerable, enumerating first the items in the existing enumerable</returns>
public static IEnumerable<T> ConcatItem<T>(this IEnumerable<T> target, T item)
{
if (null == target) throw new ArgumentException(nameof(target));
foreach (T t in target) yield return t;
yield return item;
}
/// <summary>
/// Inserts the specified element at the start of the IEnumerable.
/// </summary>
/// <typeparam name="T">The type of elements the IEnumerable contans.</typeparam>
/// <param name="target">The IEnummerable.</param>
/// <param name="item">The item to be concatenated.</param>
/// <returns>An IEnumerable, enumerating first the target elements, and then the new element.</returns>
public static IEnumerable<T> ConcatTo<T>(this IEnumerable<T> target, T item)
{
if (null == target) throw new ArgumentException(nameof(target));
yield return item;
foreach (T t in target) yield return t;
}
}
Run Code Online (Sandbox Code Playgroud)
或者,使用隐式创建的数组.(使用params关键字)这样您就可以调用方法一次添加一个或多个项目:
public static class Utility
{
/// <summary>
/// Adds the specified element at the end of the IEnummerable.
/// </summary>
/// <typeparam name="T">The type of elements the IEnumerable contans.</typeparam>
/// <param name="target">The target.</param>
/// <param name="items">The items to be concatenated.</param>
/// <returns>An IEnumerable, enumerating first the items in the existing enumerable</returns>
public static IEnumerable<T> ConcatItems<T>(this IEnumerable<T> target, params T[] items) =>
(target ?? throw new ArgumentException(nameof(target))).Concat(items);
/// <summary>
/// Inserts the specified element at the start of the IEnumerable.
/// </summary>
/// <typeparam name="T">The type of elements the IEnumerable contans.</typeparam>
/// <param name="target">The IEnummerable.</param>
/// <param name="items">The items to be concatenated.</param>
/// <returns>An IEnumerable, enumerating first the target elements, and then the new elements.</returns>
public static IEnumerable<T> ConcatTo<T>(this IEnumerable<T> target, params T[] items) =>
items.Concat(target ?? throw new ArgumentException(nameof(target)));
Run Code Online (Sandbox Code Playgroud)
cbp*_*cbp 13
Append
而Prepend
现在已经被添加到.NET标准框架,让您不再需要自己编写.只需这样做:
image.Layers.Append(image.ParentLayer)
Run Code Online (Sandbox Code Playgroud)
请参阅.Net Framework 2.0中的43个API,但.Net Framework 4.6.1中没有这些API?获得一系列新功能.
Jar*_*Par 12
没有一种方法可以做到这一点.最接近的是Enumerable.Concat
方法,但尝试将a IEnumerable<T>
与另一个组合IEnumerable<T>
.您可以使用以下内容使其适用于单个元素
image.Layers.Concat(new [] { image.ParentLayer });
Run Code Online (Sandbox Code Playgroud)
或者只是添加一个新的扩展方法
public static IEnumerable<T> ConcatSingle<T>(this IEnumerable<T> enumerable, T value) {
return enumerable.Concat(new [] { value });
}
Run Code Online (Sandbox Code Playgroud)
您可以使用Enumerable.Concat:
var allLayers = image.Layers.Concat(new[] {image.ParentLayer});
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
image.Layers.Concat(new[] { image.ParentLayer });
Run Code Online (Sandbox Code Playgroud)
它使用包含要添加的内容的单元素数组来连接枚举
我曾经为此做过一个很好的小功能:
public static class CoreUtil
{
public static IEnumerable<T> AsEnumerable<T>(params T[] items)
{
return items;
}
}
Run Code Online (Sandbox Code Playgroud)
现在这是可能的:
image.Layers.Append(CoreUtil.AsEnumerable(image.ParentLayer, image.AnotherLayer))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22743 次 |
最近记录: |