예제 #1
0
        internal void METHEXT_String_WordCount()
        {
            string str = ui.Input("Text", new string[] { "ja nee", "aap noot mies" });
            int    i   = str.WordCount();

            ui.Output("Aantal woorden: " + i);
        }
예제 #2
0
        internal void LINQ_Aggregate()
        {
            const string CHOICE_NAME = "n";
            const string CHOICE_SUM  = "s";
            const string CHOICE_AVG  = "a";

            string[] choices = new string[] { CHOICE_NAME, CHOICE_SUM, CHOICE_AVG };
            string   choice  = ui.Input("Choose: " +
                                        $"{CHOICE_NAME} (longest Name), " +
                                        $"{CHOICE_SUM} (Sum) or " +
                                        $"{CHOICE_AVG} (Average):", choices);

            switch (choice)
            {
            case CHOICE_NAME:

                string longestName = Aggregate1();

                ui.Output($"The longest name is {longestName}", "Result");
                break;

            case CHOICE_SUM:
                int[] i = { 1, 2, 3, 4 };

                ui.Output($"Sum: {i.Aggregate(0, (result, nextItem) => result + nextItem)}", "Result");
                break;

            case CHOICE_AVG:
                IEnumerable <int> numbers = new List <int> {
                    6, 2, -1, 8, 3
                }.Where(n => n > 0);

                decimal d = numbers.Aggregate(0, (result, nextItem) => result + nextItem, result => (decimal)result / numbers.Count());

                var numbersAsText = numbers.Select(n => n.ToString()).Aggregate("", (result, nextItem) => result + ", " + nextItem);

                ui.Output(String.Format("Average of [{0}] is {1}", numbersAsText, d), "Result");
                break;

            default:
                ui.Output("Invalid option", Helper.Constant.APP_NAME);
                break;
            }
        }