Advanced Services | Dot Notation versus Linq |
/* */ private static void Beispiel1_Array() { // klassisch string[] stringArr = { "Heinrich Mann", "Thomas Mann", "Bertolt Brecht", "Arthur Schnitzler", "Klaus Mann", "Erika Mann", "Karl Kraus" }; string[] dieManns = new string[4]; Console.WriteLine("-------------- dotnotation ----------------"); int i = 0; foreach (string s in stringArr) { if (s.EndsWith("Mann")) { dieManns[i++] = s; } } Array.Sort(dieManns); foreach (string s in dieManns) { Console.WriteLine(s); } // Linq Console.WriteLine("------------------ Linq -------------------"); //IEnumerable<string> enu = from name in stringArr where name.EndsWith("Mann") orderby name select name; // oder auch var enu = from name in stringArr where name.EndsWith("Mann") orderby name select name; foreach(string s in enu) { Console.WriteLine(s); } Console.WriteLine("------------- Linq + Lambda ---------------"); // nur List hat ein Foreach (from name in stringArr where name.EndsWith("Mann") orderby name select name).ToList().ForEach(s => Console.WriteLine(s)); } }
/* */ private static void Beispiel2_List() { // klassisch string[] stringArr = { "Heinrich Mann", "Thomas Mann", "Bertolt Brecht", "Arthur Schnitzler", "Klaus Mann", "Erika Mann", "Karl Kraus" }; List<string> stringList = stringArr.ToList(); List<string> dieManns = new List<string>(); Console.WriteLine("-------------- dotnotation ----------------"); int i = 0; foreach (string s in stringList) { if (s.EndsWith("Mann")) { dieManns.Add(s); } } dieManns.Sort(); foreach (string s in dieManns) { Console.WriteLine(s); } // Linq Console.WriteLine("------------------ Linq -------------------"); //IEnumerable<string> enu = from name in stringArr where name.EndsWith("Mann") orderby name select name; // oder auch var enu = from name in stringList where name.EndsWith("Mann") orderby name select name; foreach (string s in enu) { Console.WriteLine(s); } Console.WriteLine("------------- Linq + Lambda ---------------"); // nur List hat ein Foreach (from name in stringList where name.EndsWith("Mann") orderby name select name) .ToList().ForEach(s => Console.WriteLine(s)); }
/* */ private static void Beispiel1_AlleKlassenInMscorlib() { // klassisch + lambda Assembly mscorlib = Assembly.Load("mscorlib"); // System.Reflection List<Type> mscorlibTypes = mscorlib.GetTypes().ToList(); //Console.WriteLine(mscorlibTypes.Count); List<string> mscorlibStrings = mscorlibTypes.ConvertAll(t => t.Name); // Converter Delegate mscorlibStrings.Sort(); List<string> startsWithQ = mscorlibStrings.FindAll(s => s.StartsWith("Q")); // Predicate Console.WriteLine(); startsWithQ.ForEach(t => Console.WriteLine(t)); // Action Delegate Console.WriteLine(); // dotnotaion klassisch + lambda var list = Assembly.Load("mscorlib").GetTypes().ToList().ConvertAll(t => t.Name).FindAll(s => s.StartsWith("Q")); list.Sort(); list.ForEach(t => Console.WriteLine(t)); Console.WriteLine(); // Linq //var enu = from name in Assembly.Load("mscorlib").GetTypes() orderby name.Name select name; //enu.ToList().ForEach(s => Console.WriteLine(s.Name)); (from name in Assembly.Load("mscorlib").GetTypes() orderby name.Name select name).ToList().ForEach(s => Console.WriteLine(s.Name)); }
/* */ private static void Beispiel2_AlleExceptionsInMscorlib() { // klassisch // klassisch + lambda Assembly mscorlib = Assembly.Load("mscorlib"); // System.Reflection List<Type> mscorlibTypes = mscorlib.GetTypes().ToList(); List<Type> exceptionList = mscorlibTypes.FindAll(t => t.IsSubclassOf(typeof(System.Exception))); // Predicate List<string> exceptionStringList = exceptionList.ConvertAll(t => t.Name); // Converter Delegate exceptionStringList.Sort(); exceptionStringList.ForEach(t => Console.WriteLine(t)); // Action Delegate Console.WriteLine(); // Linq //var enu = from name in Assembly.Load("mscorlib").GetTypes() orderby name.Name select name; var enu = from name in Assembly.Load("mscorlib").GetTypes() where name.IsSubclassOf(typeof(System.Exception)) orderby name.Name select name; enu.ToList().ForEach(s => Console.WriteLine(s.Name)); // oder //(from name in Assembly.Load("mscorlib").GetTypes() where name.IsSubclassOf(typeof(System.Exception)) orderby name.Name select name).ToList().ForEach(s => Console.WriteLine(s.Name)); }
private static void Beispiel3_AlleDelegatesInMscorlib() { // klassisch // klassisch + lambda Assembly mscorlib = Assembly.Load("mscorlib"); // System.Reflection List<Type> mscorlibTypes = mscorlib.GetTypes().ToList(); List<Type> exceptionList = mscorlibTypes.FindAll(t => t.IsSubclassOf(typeof(System.MulticastDelegate))); // Predicate List<string> exceptionStringList = exceptionList.ConvertAll(t => t.Name); // Converter Delegate exceptionStringList.Sort(); exceptionStringList.ForEach(t => Console.WriteLine(t)); // Action Delegate Console.WriteLine(); // Linq //var enu = from name in Assembly.Load("mscorlib").GetTypes() orderby name.Name select name; var enu = from name in Assembly.Load("mscorlib").GetTypes() where name.IsSubclassOf(typeof(System.MulticastDelegate)) orderby name.Name select name; enu.ToList().ForEach(s => Console.WriteLine(s.Name)); // oder //(from name in Assembly.Load("mscorlib").GetTypes() where name.IsSubclassOf(typeof(System.MulticastDelegate)) orderby name.Name select name).ToList().ForEach(s => Console.WriteLine(s.Name)); }
private static void Beispiel4_AlleValueTypesInMscorlib() { // klassisch // klassisch + lambda Assembly mscorlib = Assembly.Load("mscorlib"); // System.Reflection List<Type> mscorlibTypes = mscorlib.GetTypes().ToList(); List<Type> exceptionList = mscorlibTypes.FindAll(t => t.IsSubclassOf(typeof(System.ValueType))); // Predicate List<string> exceptionStringList = exceptionList.ConvertAll(t => t.Name); // Converter Delegate exceptionStringList.Sort(); exceptionStringList.ForEach(t => Console.WriteLine(t)); // Action Delegate Console.WriteLine(); // Linq //var enu = from name in Assembly.Load("mscorlib").GetTypes() orderby name.Name select name; var enu = from name in Assembly.Load("mscorlib").GetTypes() where name.IsSubclassOf(typeof(System.ValueType)) orderby name.Name select name; enu.ToList().ForEach(s => Console.WriteLine(s.Name)); // oder //(from name in Assembly.Load("mscorlib").GetTypes() where name.IsSubclassOf(typeof(System.ValueType)) orderby name.Name select name).ToList().ForEach(s => Console.WriteLine(s.Name)); }