I have to see that in a IEnumerable
absolutely One element this snippet works:
bool hasOneElement = seq.Count () == 1
although it is not very efficient , Because calculation ()
will show the entire list, obviously, it is empty to know a list or more than 1 element in it means that it is not empty, this extended method has this short-circacting behavior ?
It should do this:
You can extend it , but I do not suggest you do this:
Public Stable Bull Includes ExclusiveOnTime & Lt; T & gt; (This is IEnumerable & lt; T & gt; source) {Use (INMMETER & lt; T & gt; Iterator = source.GetEnumerator ()) {return iterator.MoveNext () & amp; Amp; ! Iterator.MoveNext (); }}
It is like a trick that is cowardly, but probably should not be used in the production code. It is not yet clear enough that the fact is & amp; Amp & amp; Amp; Side effects in LHS; Operator is required because RHS is very fun working properly ... while having lots of fun;)
Edit: I have just seen that you have actually come up with the same thing , But for an arbitrary length. Your final return statement is incorrect though - this is ! En.MoveNext ()
should be back. Icoding
/ Ikling
A perfect method for public static (IMO), logic checking and optimization is a complete method of the Bull Kent Aquals & lt; T & gt; (This IEnumerable & lt; T & gt; source, integer count) {If (source == faucet) {New argument NullException ("source"); } If (calculation and lieutenant; 0) {new logic: otonerex exception ("counting", "count should not be negative"); } // We do not rely on objects on optimization in LINQ, like // they have changed between versions of ICollection & LT; T & gt; GenericClution = Source as icolon & lt; T & gt; If (generic collapse! = Blank) {generic collection. Calculation = calculation; } Icon non-generic collection = source as icolone; If (nonGenericCollection! = Null) {return nonGenericCollection.Count == count; } // OK, we are finally ready to do the actual work ... using (IEnumerator & lt; T & gt; iterator = source.GetEnumerator ()) {for (int i = 0; i & lt ; Count; i ++) {if (! Iterator.MoveNext ()) {return false; }} // Check that we did not get any more returns! Iterator.MoveNext (); }}
Edit: And for now functional fans, a recursive form of CountEquals
( please do not use it , it Only for giggles):
Public Static Bull Kent Aquals & lt; T & gt; (This is IEnumerable & lt; T & gt; source, int count) {if (source == null) {new argument (NullException) ("source"); } If (calculation and lieutenant; 0) {new logic: otonerex exception ("counting", "count should not be negative"); } (IEnumerator & lt; T & gt; iterator = source.GetEnumerator ()) is using IteratorCountEquals (iterator, count); }} Private Stable Bull IteratorCountEquals & lt; T & gt; (IEnumerator & lt; T & gt; Iterator, integer number) {return calculation == 0? ! Iterator.MoveNext (): iterator.MoveNext () & amp; Amp; IteratorCountEquals (iterator, calculation - 1); } Edit: Note that for LINQ to do something like SQL, you should use the simple calculation ()
approach - because it does this Will allow for the actual results instead of the database.
Comments
Post a Comment