Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
How to get accepted in Fileice (200% Working) 22/2012
Premium Account
FILE HOSTS PREMIUM ACCOUNT
ALL FILE HOST PREMIUM ACCOUNTS
Zynga Slingo Trainer v5.12
iTunes Gift Card Generator V3.1 2012
Diablo 3 GOLD Coins FREE
Working PS3 Jailbreak 3.65 And 3.66
ExtaBit Premium Accounts and Cookies
Steam Wallet Hack - Money Adder & Hack v3
Popular
XBOX POINTS GENERATOR - MICROSOFT POINTS GENERATOR v1.2012
11 may 2012 premium uploading accounts 100% working
Free Microsoft Points
Free Microsoft Points - Microsoft Points Generator - Xbox Live Codes 2012
Car Town Free Blue Points Hack
Free CarTown Blue Points Generator and CarTown Templates
Better way to get content via jQuery $.get()
Free Microsoft Points
Simple Days Purger
Sharecash Downloader Bypass Surveys New 05/2012
Pastable version of
Cached IEnumerable<T>
<pre class='prettyprint' language='cs'>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { /// <summary> /// Wraps an IEnumerable&lt;T&gt; and provides a thread-safe means of caching the values."/> /// </summary> /// <typeparam name="T"></typeparam> class ThreadSafeCachedEnumerable<T> : IEnumerable<T> { // An enumerator from the original IEnumerable<T> private IEnumerator<T> enumerator; // The items we have already cached (from this.enumerator) private IList<T> cachedItems = new List<T>(); public ThreadSafeCachedEnumerable(IEnumerable<T> enumerable) { this.enumerator = enumerable.GetEnumerator(); } #region IEnumerable<T> Members public IEnumerator<T> GetEnumerator() { // The index into the sequence int currentIndex = 0; // We will break with yield break while (true) { // The currentIndex will never be decremented, // so we can check without locking first if (currentIndex < this.cachedItems.Count) { var current = this.cachedItems[currentIndex]; currentIndex += 1; yield return current; } else { // If !(currentIndex < this.cachedItems.Count), // we need to synchronize access to this.enumerator lock (enumerator) { // See if we have more cached items ... if (currentIndex < this.cachedItems.Count) { var current = this.cachedItems[currentIndex]; currentIndex += 1; yield return current; } else { // ... otherwise, we'll need to get the next item from this.enumerator.MoveNext() if (this.enumerator.MoveNext()) { // capture the current item and cache it, then increment the currentIndex var current = this.enumerator.Current; this.cachedItems.Add(current); currentIndex += 1; yield return current; } else { // We reached the end of the enumerator - we're done yield break; } } } } } } #endregion #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } #endregion } }</pre> <a href="http://www.refactormycode.com/codes/945-cached-ienumerable-t" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>