oliverbooth / X10D

Extension methods on crack.
https://oliverbooth.github.io/X10D/
MIT License
28 stars 2 forks source link

AsArray/AsEnumerable #47

Closed oliverbooth closed 2 years ago

oliverbooth commented 2 years ago

Type

T

Extension method signature Provide the full method signature here, without parameter names, but do not include the leading this T x parameter. Example:

T[] AsArray<T>(this T value)
// and
IEnumerable<T> AsEnumerable<T>(this T value)
Parameters Parameter Type Description
value T The value to encapsulate.

Description Encapsulates the value into an array of length 1 containing the value. or Yields the value into an IEnumerable<T> with one element.

Benefits To save having to encapsulate as new Foo[] { myFoo } when object is known singleton where array is expected, myFoo.AsArray() or myFoo.AsEnumerable() allows for contiguous method chaining.

Drawbacks AsEnumerable could conflict with LINQ's AsEnumerable, this may be something to consider.

(Optional) Implementation example

{
    return new[] {value};
    // or
    yield return value;
}