Beispiel #1
0
        static void Main(string[] args)
        {
            Programmer proger = new Programmer("Саня", 120);
            List <int> list1  = new List <int>();

            list1.Add(1);
            list1.Add(2);
            list1.Add(3);
            Console.WriteLine("Элементы списка 1:");
            foreach (int i in list1)
            {
                Console.WriteLine(i);
            }
            List <int> list2 = new List <int>();

            list2.Add(4);
            list2.Add(5);
            list2.Add(6);
            Console.WriteLine("Элементы списка 2:");
            foreach (int i in list2)
            {
                Console.WriteLine(i);
            }
            proger.Delete += () => { Remove(list1); Console.WriteLine("Вызвано событие 'Удалить'"); };
            proger.NameOut();
            Console.WriteLine("Элементы списка 1:");
            foreach (int i in list1)
            {
                Console.WriteLine(i);
            }
            proger.Mut += () => { Reverse(list2); Console.WriteLine("Вызвано событие 'Мутировать'"); };
            proger.IntelligencePlusOne();
            Console.WriteLine("Элементы списка 2:");
            foreach (int i in list2)
            {
                Console.WriteLine(i);
            }
            string str = "Hey, hOw: aRE\" yoU?";

            Console.WriteLine("------------Делегаты и пользовательские функции обработки строк------------");
            Console.WriteLine("------Простой вызов функций------");
            DeletePunktuation(str);
            DeleteSpaces(str);
            ToUpperCase(str);
            ToLowerCase(str);
            SpaceIntoDog(str);
            Console.WriteLine("------Вызов функций с помощью делегатов------");
            Console.WriteLine("---Делегат Action---");
            Action <string> firstDel;

            firstDel  = DeletePunktuation;
            firstDel += DeleteSpaces;
            firstDel += ToUpperCase;
            firstDel(str);
            Console.WriteLine("---Делегат Func---");
            Func <string, string> secondDel;

            secondDel  = ToLowerCase;
            secondDel += SpaceIntoDog;
            secondDel(str);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Programmer programmer = new Programmer();

            programmer.pl += delegate(object sender, ProgramLanguage pl) {
                Programmer d = (Programmer)sender;
                d.action(); // вызов замыкания
                string str = new string('-', 40);
                str += "\n";
                str += pl.name;
                for (int j = 0; j < pl.propertiesCount; j++)
                {
                    str += "\n > " + pl.properties[j];
                }
                Console.WriteLine("\n" + str);
            };

            int z = 0, buffer1 = 0, buffer2 = 0;

            string[] buffer;
            string   TestString = "";

            Func <string, string> f = null;

            do
            {
                Console.Clear();
                Console.Write("1. Rename\n2. Add properties\n3. Task2\n0. Exit\n");
                Console.Write(">/");
                try {
                    z = Convert.ToInt32(Console.ReadLine());
                }
                catch (FormatException) {
                    Console.WriteLine("\nError: FormatException");
                    Console.Write("\nPress any key to continue...");
                    Console.ReadKey();
                }
                Console.Clear();
                switch (z)
                {
                default:
                    Console.Write("Error: wrong number\n");
                    Console.Write("\nPress any key to continue...");
                    Console.ReadKey();
                    break;

                case 1:
                    Console.WriteLine(programmer.ToString());
                    try {
                        Console.Write("Enter index: ");
                        buffer1 = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Enter number of properties: ");
                    }
                    catch (FormatException) {
                        Console.WriteLine("\nError: FormatException");
                        Console.Write("\nPress any key to continue...");
                        Console.ReadKey();
                    }
                    buffer = new string[1];
                    Console.Write(" >Enter new name: ");
                    buffer[0] = Console.ReadLine();
                    programmer.Rename(buffer1, buffer[0]);
                    Console.Write("\nPress any key to continue...");
                    Console.ReadKey();
                    break;

                case 2:
                    Console.WriteLine(programmer.ToString());
                    try {
                        Console.Write("Enter index: ");
                        buffer1 = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Enter number of properties: ");
                        buffer2 = Convert.ToInt32(Console.ReadLine());
                        if (buffer2 <= 0)
                        {
                            throw new FormatException("must be positive");
                        }
                    }
                    catch (FormatException) {
                        Console.WriteLine("\nError: FormatException");
                        Console.Write("\nPress any key to continue...");
                        Console.ReadKey();
                    }
                    buffer = new string[buffer2];
                    for (int i = 0; i < buffer2; i++)
                    {
                        Console.Write(" >Enter property " + i + ": ");
                        buffer[i] = Console.ReadLine();
                    }
                    programmer.AddProperties(buffer1, buffer);
                    Console.Write("\nPress any key to continue...");
                    Console.ReadKey();
                    break;

                case 3:
                    Console.Write("Enter string: ");
                    TestString = Console.ReadLine();

                    f          = SomeClass.ToUpperCase;
                    TestString = f(TestString);
                    f          = SomeClass.DeleteDots;
                    TestString = f(TestString);
                    f          = SomeClass.DeleteColons;
                    TestString = f(TestString);
                    f          = SomeClass.DeleteSemicolons;
                    TestString = f(TestString);
                    f          = SomeClass.SplitWordsByComma;
                    TestString = f(TestString);

                    Console.WriteLine("\nResult: " + TestString);
                    Console.Write("\nPress any key to continue...");
                    Console.ReadKey();
                    break;

                case 0:
                    Console.Write("\nPress any key to continue...");
                    Console.ReadKey();
                    break;
                }
            } while (z != 0);
        }