• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/ODAw

Enumerable that does not give multiple side effects on multiple enumerations


6
760 2 0 0 0 0

public class FixedEnumerable<T> : IEnumerable<T>
{
    private IList<T> _materialized;
    private readonly IEnumerable<T> _enumerable;

    public FixedEnumerable(IEnumerable<T> enumerable)
    {
        _enumerable = enumerable;
    }
    public FixedEnumerable(IList<T> list)
    {
        _materialized = list;
    }

    public IEnumerator<T> GetEnumerator()
    {
        if (_materialized == null)
            _materialized = _enumerable.ToList();

        return _materialized.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public static implicit operator FixedEnumerable<T>(List<T> list)
    {
        return new FixedEnumerable<T>(list);
    }
}

namespace System.Linq
{
    public static class FixedEnumerableExtentions
    {
        public static FixedEnumerable<T> ToFixed<T>(this IEnumerable<T> enumerable)
        {
            return new FixedEnumerable<T>(enumerable);
        }
    }

}

Share: twitter | facebook   Action: used | fork | flag

eager

Mark 'eager' tag as 'like'

Mark 'eager' tag as 'ignore'

enumeration

Mark 'enumeration' tag as 'like'

Mark 'enumeration' tag as 'ignore'

ienumerable

Mark 'ienumerable' tag as 'like'

Mark 'ienumerable' tag as 'ignore'

lazy

Mark 'lazy' tag as 'like'

Mark 'lazy' tag as 'ignore'


 @adam.lith "I'm a Forker"
400
August 30, 2011 3:39 PM
edited February 03, 2012 11:31 AM

Fork

 Enumerable that does not give multiple side effects on multiple enumerations -  @adam.lith Tuesday 30, 2011 3:39 PM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010