- Where Developers Learn, Share, & Build Careers
I have a This works, but looks awesome: There are several LINQ extension methods. for example (I have liked it for my own need): The best way to get your requirement depends on how strong this rule is: I want to get the first element if only If = 1 If it is extraordinary that contains more than one element in the sequence, then use If it is important that you get first element and you never want another element, better If the element's index is important in the sequence and you (at present) need the first element, but it can change in future, list with an encoded
lambda or some good build?
var blah = new list & lt; Object & gt; () {New object ()}; Var item = (blah.Count == 1? Blah [0]: zero);
default will be returned, then returned it
default
default ; T & gt; will be returned, if there are not many elements in the sequence will be returned
var item = blah.ElementAtOrDefault (0);
SingleOrDefault and you will be notified (error Log) if it gets out of hand.
use first and default because It has a meaningful name.
ElementAtOrDefault Use.
Comments
Post a Comment