Im Laufe der Zeit habe ich mir ein kleines Repertoire an Extensions und Hilfsfunktionen in C# und LINQ angesammelt. Hier meine Favoriten:
1. ForEach()
2. IsEmpty()
3. GetSQLDate()
4. Stopwatch extensions
5. StringBuilder extensions
6. Merging lists
7. Left/Mid/Right
public static void ForEach<T> (this IEnumerable<T> enumeration, Action<T> action) { foreach (T item in enumeration) { action(item); } }
Beispiel:
int[] foo = { 1, 2, 3, 4, 5 }; foo.ForEach(p => p *= p); // foo enthält nun: { 1, 4, 9, 16, 25 }
(Bitte verschont mich mit Kommentaren bzgl. des Gebrauchs von foreach-Schleifen.)
public static Boolean IsEmpty<T> (this IEnumerable<T> source) { try { if (source == null) return true; return !source.Any(); } catch { return true; } }
Beispiel:
int[] foo = { 1, 2, 3, 4, 5 }; bool bar; bar = foo.IsEmpty(); // bar ist nun false foo = null; bar = foo.IsEmpty(); // bar ist nun true
3. GetSQLDate()
Wandelt ein Datumswert in das SQL-Server Standardformat um.
public static string GetSQLDate(this DateTime? _d) { if (_d.HasValue) return string.Concat("'", _d.Value.ToString("yyyy-MM-dd HH:mm:ss"), "'"); else return "NULL"; }
Beispiel:
DateTime foo = DateTime.Now; string SQL = string.Format("SELECT ID FROM dbo.dummy_table WHERE datetime={0};", foo.GetSQLDate()); /* Wäre "foo" ein DateTime?, also nullable, würde nach "NULL" gesucht werden. Das ist bei SELECT-Abfragen ggf. unerwünscht, aber bei INSERT-Abfragen durchaus gewollt. */
public static string StopAndGetSecondsString(this Stopwatch stopwatch) { stopwatch.Stop(); double Seconds = stopwatch.ElapsedTicks / (float)Stopwatch.Frequency; return String.Format("{0:0.0000}", Seconds); } public static void Restart(this Stopwatch stopwatch) { stopwatch.Stop(); stopwatch.Reset(); stopwatch.Start(); } public static Stopwatch GetStartedStopwatch() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); return stopwatch; }
Beispiel:
Stopwatch sw = GetStartedStopwatch(); // Stopwatch läuft bereits! // Zeitintensive Operationen hier einfügen... Console.WriteLine(sw.StopAndGetSecondsString()); // Stoppen und Ausgabe auf Console. sw.Restart(); // Zurücksetzen und wieder starten.
public static StringBuilder AppendAllElements(this StringBuilder _S, IEnumerable<string> _IE) { _IE.ForEach(s => _S.AppendLine(s)); return _S; }
Beispiel:
StringBuilder sb = new StringBuilder(); List<string> s = new List<string> () { "a", "b", "c", "d", "e" }; sb.AppendAllElements(s); // füge alle Objekte aus "s" an den StringBuilder (zeilengetrennt!) Console.WriteLine(sb.ToString()); // Ausgabe auf die Console
public static IEnumerable<string> Merge(this IEnumerable<IEnumerable<string> > enumeration) { List<string> _l = new List<string> (); foreach (IEnumerable<string> item in enumeration) { _l.AddRange(item); } return _l.AsEnumerable(); }
Beispiel:
List<List<string> > s1 = new List<List<string> > (); // Jagged bzw. mehrdimensionale Liste! List<string> s2 = new List<string> () { "a", "b", "c", "d", "e" }; List<string> s3 = new List<string> () { "D", "1", "V", "F", "Q" }; s1.Add(s2); s1.Add(s3); List<string> s4 = s1.Merge().ToList(); // s4 enthält nun: // "a", "b", "c", "d", "e", "D", "1", "V", "F", "Q"
Wie ich später herausgefunden habe, geht das ganze auch viel einfacher ohne Extension, nur mit SelectMany() :
Beispiel:
List<List<string> > s1 = new List<List<string> > (); // Jagged bzw. mehrdimensionale Liste! List<string> s2 = new List<string> () { "a", "b", "c", "d", "e" }; List<string> s3 = new List<string> () { "D", "1", "V", "F", "Q" }; s1.Add(s2); s1.Add(s3); List<string> s4 = s1.SelectMany(x => x).ToList(); // s4 enthält nun: // "a", "b", "c", "d", "e", "D", "1", "V", "F", "Q"
7. Left/Mid/Right
Stringoperationen für alle VB-Umsteiger:
public static string Left(this string param, int length) { return param.Substring(0, length); } public static string Right(this string param, int length) { return param.Substring(param.Length - length, length); } public static string Mid(this string param, int startIndex, int length) { return param.Substring(startIndex, length); ; } public static string Mid(this string param, int startIndex) { return param.Substring(startIndex); }