static void Main(string[] args)
        {
            DisplayTemplate.Header("Lambda Expressions");

            List <int> numbers = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };

            // Print even numbers in the list.
            foreach (int number in numbers)
            {
                if (number % 2 == 0)
                {
                    Console.Write(number + " ");
                }
            }
            Console.WriteLine();
            // Above method fits in one line, but it is still combination of foreach and if.
            // We can use FindAll method of List in combination with delegate.
            foreach (int number in numbers.FindAll(delegate(int number) { return(number % 2 == 0); }))
            {
                Console.Write(number + " ");
            }
            Console.WriteLine();
            // Note that name of variable in foreach, which is 'number', can be either same or different from the name of variable in delegate.

            // delegate is a little confusing and hard to remember. To make it short, let's use Lambda Expression.
            foreach (int number in numbers.FindAll(number => number % 2 == 0))
            {
                Console.Write(number + " ");
            }
            Console.WriteLine();

            DisplayTemplate.Footer();
        }
        static void Main(string[] args)
        {
            DisplayTemplate.Header("Multi-Threading Programming");

            int[]    numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            string[] flowers = { "Jasmine", "Rose", "Sunflower", "Lotus", "Lily" };

            Thread numberThread = new Thread(new ParameterizedThreadStart(printNumbers));
            Thread flowerThread = new Thread(new ParameterizedThreadStart(printFlowers));

            numberThread.Start(numbers);
            flowerThread.Start(flowers);

            DisplayTemplate.Footer();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            // Instead of copying and pasting the header and footer of program all the time
            // Using a template which is defined in DisplayTemplate class of Templates namespace
            // This reduces redundant writing of code. To use this class, add Templates project as a reference.

            DisplayTemplate.Header("Fun With Collections");

            // There are many in-built collections.
            // Use Generic collections to avoid issues.

            // List is a dynamic array. The size can increase.
            Console.WriteLine("Integer type List Example");
            List <int> intArr = new List <int>();

            for (int i = 0; i < 10; i++)
            {
                intArr.Add(i);
            }
            intArr.ForEach(num => Console.Write(num + " ")); // List can iterate over elements and use lambda expression.
            Console.WriteLine();
            intArr.RemoveAt(5);
            intArr.ForEach(num => Console.Write(num + " "));
            Console.WriteLine("\n");

            // You can also make a General list. This list contains int, string and double.
            List <object> genArr = new List <object>();

            genArr.Add(21);
            genArr.Add("Mark");
            genArr.Add(0.5);
            Console.WriteLine("General List Example");
            genArr.ForEach(num => Console.WriteLine(num + " : " + num.GetType()));
            Console.WriteLine();

            // An example of Stack
            Stack <int> stack = new Stack <int>();

            stack.Push(1);
            stack.Push(2);
            stack.Push(3);
            Console.WriteLine("Stack Example");
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Peek());
            Console.WriteLine();

            // An example of Queue
            Queue <int> queue = new Queue <int>();

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
            Console.WriteLine("Queue Example");
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Peek());
            Console.WriteLine();

            // An example of Dictionary. Dictionary is a key-value pair. Key must be unique.
            Console.WriteLine("Dictionary Example");
            Dictionary <int, string> courses = new Dictionary <int, string>()
            {
                [101] = "Math", [102] = "Science"
            };

            courses.Add(103, "Computers");
            courses.Add(104, "History");

            // Iterate using keys
            foreach (int key in courses.Keys)
            {
                Console.WriteLine(key + " : " + courses[key]);
            }
            Console.WriteLine();

            DisplayTemplate.Footer();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            DisplayTemplate.Header("Language Integrated Queries (LINQ)");
            DisplayTemplate.Header("LINQ to Objects");

            string[]             games  = { "Skyrim", "Uncharted 4", "The Last Of Us", "The Witcher III", "Control" };
            IEnumerable <string> subset = from game in games where game.Contains(" ") orderby game select game;

            foreach (string game in subset)
            {
                Console.WriteLine(game);
            }
            Console.WriteLine();

            // Another way is to use extensions
            subset = games.Where(game => game.Contains("The")).OrderBy(game => game).Select(game => game);
            foreach (string game in subset)
            {
                Console.WriteLine(game);
            }
            Console.WriteLine();

            // Instead of using IEnumerable, we can also use var
            var sorted = games.OrderBy(game => game).Select(game => game);

            foreach (string game in sorted)
            {
                Console.WriteLine(game);
            }
            Console.WriteLine();

            // LINQ queries are dynamic. Which means, there is no need to define LINQ queries again and again.
            games[1] = "God Of War";
            foreach (string game in sorted)
            {
                Console.WriteLine(game);
            }
            Console.WriteLine();

            // Data from LINQ queries can be stored in a collection.
            int[] numbers     = { 1, 2, 5, 7, 8, 23, 12, 45, 76, 32, 124, 6, 53, 4, 34 };
            int[] evenNumbers = numbers.Where(number => number % 2 == 0).OrderBy(number => - number).Select(number => number).ToArray <int>();
            foreach (int number in evenNumbers)
            {
                Console.Write(number + " ");
            }
            Console.WriteLine();
            // Note that values do not change after storing in a collection.
            numbers[1] = 25;
            foreach (int number in evenNumbers)
            {
                Console.Write(number + " ");
            }
            Console.WriteLine("\n");

            // LINQ queries over objects of custom classes
            List <Student> students = new List <Student>()
            {
                new Student(101, "Mark", 10),
                new Student(105, "John", 12),
                new Student(104, "Jack", 10),
                new Student(102, "Simon", 12),
                new Student(103, "Phillip", 10),
            };

            Student[] tenthGradeStudents = students.Where(student => student.Grade == 10).OrderBy(student => student.RollNumber).Select(student => student).ToArray <Student>();
            foreach (Student student in tenthGradeStudents)
            {
                Console.WriteLine(student.Name);
            }
            Console.WriteLine();

            // In above example, I am creating array of Student, even though I am just printing names.
            // This is very inefficient as Student array takes a lot more space than a string array.
            // A better way is to retrieve only required data.
            string[] tenthGradeStudentsNames = students.Where(student => student.Grade == 10).OrderBy(student => student.RollNumber).Select(student => student.Name).ToArray <string>();
            foreach (string student in tenthGradeStudentsNames)
            {
                Console.WriteLine(student);
            }
            Console.WriteLine();

            DisplayTemplate.Footer();
        }