Created
July 17, 2009 23:03
-
-
Save skoon/149329 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace CodeSamplesForArena.net | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var selector = new Selector(); | |
var valueList = selector.createValueEnumerable(); | |
var valueListClass = new ValueList(valueList); | |
var evenList = valueList.Where(x => { return selector.Even(x.value); }); | |
var oddList = valueListClass.Where(x => x.value % 2 > 0); | |
Action<int> action = x => { Console.WriteLine(x); }; | |
OutputEnumerable(evenList, action); | |
OutputEnumerable(oddList, action); | |
Console.ReadKey(); | |
} | |
private static void OutputEnumerable(IEnumerable<Value> enumerableValue, Action<int> action) | |
{ | |
foreach (Value v in enumerableValue) | |
{ | |
action(v.value); | |
} | |
} | |
} | |
public class Value | |
{ | |
public int value { get; set; } | |
public string name { get; set; } | |
} | |
public class Selector | |
{ | |
public Func<int, bool> Even { get { return x => { return (x % 2 == 0); }; } } | |
public IEnumerable<Value> createValueEnumerable() | |
{ | |
var list = new List<Value>(); | |
for (int i = 0; i < 100; i++) | |
list.Add(new Value { name = "foo" + i, value = i }); | |
return list; | |
} | |
} | |
public class ValueList : IEnumerable<Value> | |
{ | |
private List<Value> _valueList = new List<Value>(); | |
public ValueList(IEnumerable<Value> values) | |
{ | |
_valueList.AddRange(values); | |
} | |
public void Add(Value value) | |
{ | |
_valueList.Add(value); | |
} | |
public IEnumerator<Value> GetEnumerator() | |
{ | |
foreach (Value v in _valueList) | |
yield return v; | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment