c# - Passing a single item as IEnumerable<T> -
Is there a way to expect one type of T
one method ? IEnumerable & lt; T & gt;
parameter? Language C # is Framework version 2.0.
Currently I am using an accessory method (this is .Net 2.0, so I have a whole group of casting / project supporting methods similar to LINQ), but it seems silly only:
Public Stable Class IEnumerableExt {// Usage: IEnumerableExt.FromSingleItem (someObject); Public stable IEnumerable & lt; T & gt; From Signal Atom & lt; T & gt; (T items) {yield returns item; }}
The other way is definitely a list & lt; T & gt;
or Aare
to create and populate IEnumerable & lt; T & gt;
. [edit] It can be named as an extension method:
public static class IEnumerableExt {// Usage: someObject.SingleItemAsEnumerable (); Public stable IEnumerable & lt; T & gt; SingalItemAmAmaryable & lt; T & gt; (It's T item) {yield returns item; }}
Am I missing something here?
[edit 2] We found someObject.ield ()
(as suggested in the comments below @ Peter) this extension method Is the best name, primarily in brief, so here XML is with comment, if someone wants to capture it:
public static class IEnumerableExt {/// & lt; Summary & gt; /// This object has an IEnumerable & amp; Lt; T & amp; G; //// One item consisting of /// & lt; / Summary & gt; /// & lt; Typename name = "t" & gt; Type of object & lt; / Typeparam & gt; /// & lt; Param name = "item" & gt; Examples that will be wrapped. & Lt; / Param & gt; /// & lt; Returns & gt; An INMMerable & amp; Lt; T & amp; G; Consist of one item & lt; / Returns & gt; Public stable IEnumerable & lt; T & gt; Yield & LT; T & gt; (It's T item) {yield returns item; }}
Your helpful method is the safest way to do this, IMO if you have any If you enter a list or an array, then a dishonest code can remove the content by removing it, which causes strange behavior in some situations. You can use a collection to read only, but there is a possibility of more raping involved. I think your solution is as streamlined.
Comments
Post a Comment