Beschreibung:
Main-Thread hier!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
namespace ExtensionLibrary
{
public static class IEnumerableExtensionMethods
{
// Extensions Methods
#region +^ Implode<T>(this IEnumerable<T> enumerable, Int32 start, Int32 end, String separator)
/// <summary>
/// Implodes the <paramref name="enumerable"/> to a string containing the string representants of each item, seperated by <paramref name="seperator"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable">The enumerable.</param>
/// <param name="start">The start index.</param>
/// <param name="end">The end index.</param>
/// <param name="separator">The separator.</param>
/// <returns></returns>
public static String Implode<T>(this IEnumerable<T> enumerable, Int32 start, Int32 end, String separator)
{
StringBuilder sb = new StringBuilder();
for (Int32 i = start; i < end; i++)
{
sb.Append(enumerable.ElementAt(i));
if (i < end - 1)
{
sb.Append(separator);
}
}
return sb.ToString();
}
#endregion
#region +^ Implode<T>(this IEnumerable<T> enumerable, String separator)
/// <summary>
/// Implodes the <paramref name="enumerable"/> to a string containing the string representants of each item, seperated by <paramref name="seperator"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable">The enumerable.</param>
/// <param name="separator">The separator.</param>
/// <returns></returns>
public static String Implode<T>(this IEnumerable<T> enumerable, String separator)
{
return Implode(enumerable, 0, enumerable.Count(), separator);
}
#endregion
#region +^ ImplodeFormatted<T>(this IEnumerable<T> enumerable, Int32 start, Int32 end, String format)
/// <summary>
/// Implodes the <paramref name="enumerable"/> to a string containing the formatted string representants of each item.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable">The enumerable.</param>
/// <param name="start">The start index.</param>
/// <param name="end">The end index.</param>
/// <param name="seperatorFormat">The seperator format.</param>
/// <returns></returns>
public static String ImplodeFormatted<T>(this IEnumerable<T> enumerable, Int32 start, Int32 end, String seperatorFormat)
{
StringBuilder sb = new StringBuilder();
for (Int32 i = start; i < end; i++)
{
sb.AppendFormat(seperatorFormat, enumerable.ElementAt(i));
}
return sb.ToString();
}
#endregion
#region +^ ImplodeFormatted<T>(this IEnumerable<T> list, String format)
/// <summary>
/// Implodes the <paramref name="enumerable"/> to a string containing the formatted string representants of each item.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable">The enumerable.</param>
/// <param name="format">The format.</param>
/// <returns></returns>
public static String ImplodeFormatted<T>(this IEnumerable<T> enumerable, String format)
{
StringBuilder sb = new StringBuilder();
foreach (T item in enumerable)
{
sb.AppendFormat(format, item);
}
return sb.ToString();
}
#endregion
}
}
Implode und die verschiedenen Überladungen bewirken Folgendes:
var ints = new List<Int32>(new Int32[] { 12, 13, 14, 15 });
Console.WriteLine(ints.Implode(", "));
// Ausgabe:
// "12, 13, 14, 15"
Console.WriteLine(ints.ImplodeFormattet("Nummer {0}, "));
// Ausgabe:
// "Nummer 12, Nummer 13, Nummer 14, Nummer 15"
Gruß, Christian.
Schlagwörter: Extension Methods Extensionmethods Sammlung IEnumerable
Diese zwei Methoden sind denke ich selbsterklärend:
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
{
foreach (var item in enumerable)
action(item);
}
public static void For<T>(this IEnumerable<T> enumerable, Action<T, int> action)
{
int i = 0;
enumerable.ForEach(t => action(t, i++));
}
Komisch eigentlich, dass in 2.0 List<T> eine identische Foreach-Methode hat, das Dictionary hingegen nicht.
Wie willst du das in einem Dictionary vernünftig lösen? Das ist ja eh schon komfortabel:
Dictionary<string, string> d = new Dictionary<string, string>();
d.Values.ToList().ForEach(new Action<string>(delegate(string s) { s.Trim(); }));
mfg Markus
@Schamese:
IEnumerable.ElementAt(), ich glaub, das ist ziemlich lahm. Weil zählt jedes mal die ganze Enumeration durch.
Ansonsten hättich noch für IList allgemein:
public static void Swap(this IList list, int i1, int i2) {
object tmp = list[i1];
list[i1] = list[i2];
list[i2] = tmp;
}
und das man Arrays sortieren kann wie List(Of T), so als Member-Funktion
public static void Sort<T>(this T[] array) where T : IComparable {
Array.Sort(array);
}
public static void Sort<T>(this T[] array, Comparison<T> comparison) {
Array.Sort(array, comparison);
}
Der frühe Apfel fängt den Wurm.
Hallo zusammen,
siehe auch [ExtensionMethods] LeftOuterJoin für IQueryable (und IEnumerable).
herbivore