Basically I am trying to do something like this:
image.Layers
which returns an IEnumerable for all layers except the Parent
layer, but in some cases, I just want to do:
image.Layers.With(image.ParentLayer);
because it's only used in a few places compared to the 100s of the usual usage which is satisfied by image.Layers
. That's why I don't want to make another property that also returns the Parent
layer.
One way would be to create a singleton-sequence out of the item (such as an array), and then Concat
it onto the original:
image.Layers.Concat(new[] { image.ParentLayer } )
If you're doing this really often, consider writing an Append
(or similar) extension-method, such as the one listed here, which would let you do:
image.Layers.Append(image.ParentLayer)
.NET Core Update (per the "best" answer below):
Append
andPrepend
have now been added to the .NET Standard framework, so you no longer need to write your own. Simply do this:image.Layers.Append(image.ParentLayer)