Visual Studio 2008 provides many extension method for enumerable, extension method in this post is additional extension that I wrote while developing programs.
public static IEnumerable<T> Repeat<T>(this IEnumerable<T> source, int num) {
for (int x = 0; x < num; x++) {
using (IEnumerator<T> source_enum = source.GetEnumerator()) {
while (source_enum.MoveNext())
yield return source_enum.Current;
}
}
}
public static IEnumerable<T> AsEnumerable<T>(T item) {
yield return item;
}
Repeat method is to reiterate enumerable for specific amount of times. This method is unlike built-in Repeat method of VS 2008. The built-in Repeat method repeats a single item, but this Repeat method repeats an enumerable.
var result = new[] { 1, 2, 3 }.Repeat(3);
//result = {1, 2, 3, 1, 2, 3, 1, 2, 3}
Example above makes the repeat of array {1, 2, 3} for 3 times.
AsEnumerable method makes single item to an enumerable of single element. This method is unlike built-in AsEnumerable method of VS 2008. The built-in AsEnumerable method just cast an enumerable to IEnumerable interface. This AsEnumerable method is not casting, but wrap single item with enumerable.
var result = new[] { 1, 2, 3 }.Concat(Enumerable2.AsEnumerable(4));
//result = {1, 2, 3, 4}
Example above array of {1, 2, 3} is able to concatenate with enumerable of 4.
Some more functions:
public static bool All<T>(this IEnumerable<T> source, Func<T, int, bool> predicate) {
int i = 0;
foreach (T item in source) {
if (!predicate(item, i++))
return false;
}
return true;
}
public static bool Any<T>(this IEnumerable<T> source, Func<T, int, bool> predicate) {
int i = 0;
foreach (T item in source) {
if (predicate(item, i++))
return true;
}
return false;
}
Both All and Any methods are the same with built-in All/Any methods, but the different is these methods provide index used in predicate function.
var result = new[] { 0, 1, 2 }.All((x, i) => x == i);
//result = true
var result2 = new[] { 2, 1, 0 }.Any((x, i) => x == i);
//result2 = true
Example above, All method tests every element's value is equal to index, which is true. Any method tests any element's value is equal to index, it is true because number 1's value is equal to index.
Last 2 functions, these functions are not extension of enumerable, but they are related to enumerable.
public static IEnumerable<T> Enum<T>() {
return System.Enum.GetValues(typeof(T)).Cast<T>();
}
public static T[] Append<T>(this T[] source, T item) {
int len = source.Length;
T[] ret = new T[len + 1];
source.CopyTo(ret, 0);
ret[len] = item;
return ret;
}
Enum method creates enumerable from enum.
var result = Enumerable2.Enum<DayOfWeek>();
//result = {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
Example above, Enum method creates enumerable of DayOfWeek type. Values range from Sunday to Saturday.
Append method appends an item to an array.
var result = new[] { 1, 2, 3 }.Append(4);
//result = {1, 2, 3, 4}
Example above 4 is appended to array of {1, 2, 3} and results to new array of {1, 2, 3, 4}.
Next post is still be on more Extension Methods.
วันศุกร์ที่ 18 มกราคม พ.ศ. 2551
Extension Methods for Enumerable
เขียนโดย Chaow ที่ 16:11
ป้ายกำกับ: enumerable, extension
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น