Exemple #1
0
        static void Main()
        {
            Console.WriteLine("***** Fun with Custom Generic Methods *****\n");

            // Поменяем два целых числа
            int a = 10, b = 90;

            Console.WriteLine("Before swap: {0}, {1}", a, b);
            MyGenericMethods.Swap(ref a, ref b);
            Console.WriteLine("After swap: {0}, {1}", a, b);
            Console.WriteLine();

            // Поменяем местами две строки
            string s1 = "Hello", s2 = "There";

            Console.WriteLine("Before swap: {0} {1}!", s1, s2);
            MyGenericMethods.Swap(ref s1, ref s2);
            Console.WriteLine("After swap: {0} {1}!", s1, s2);
            Console.WriteLine();

            // Компилятор выведет тип автоматически по контексту
            bool b1 = true, b2 = false;

            Console.WriteLine("Before swap: {0}, {1}", b1, b2);
            MyGenericMethods.Swap(ref b1, ref b2);
            Console.WriteLine("After swap: {0}, {1}", b1, b2);
            Console.WriteLine();

            // Здесь нужно указать параметр типа явно, потому что метод не принимает параметров
            MyGenericMethods.DisplayBaseClass <int>();
            MyGenericMethods.DisplayBaseClass <string>();

            Console.ReadLine();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            double a = 90;
            double b = 10;

            string c = "World";
            string d = "Hello";

            bool a1 = true;
            bool b1 = false;

            Console.WriteLine("Before a = {0}, b = {1}", a, b);
            MyGenericMethods.Swap <double>(ref a, ref b);
            Console.WriteLine("After a = {0}, b = {1}", a, b);

            Console.WriteLine("Before c = {0}, d = {1}", c, d);
            MyGenericMethods.Swap <string>(ref c, ref d);
            Console.WriteLine("After c = {0}, d = {1}", c, d);

            MyGenericMethods.Swap <bool>(ref a1, ref b1);

            MyGenericMethods.DispalayBaseClass <int>();
            MyGenericMethods.DispalayBaseClass <string>();

            Console.ReadLine();
        }
Exemple #3
0
        static void InstanceLevel()
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("=> non-static custom generic methods");

            MyGenericMethods c = new MyGenericMethods();

            string s1 = "Tokyo", s2 = "Nagoya";

            Console.WriteLine($"Before swap: <{s1}>, <{s2}>");
            c.Swap <string>(ref s1, ref s2);
            c.DisplayBaseClass <string>();
            Console.WriteLine($"After  swap: <{s1}>, <{s2}>");

            Person p1 = new Person {
                FirstName = "Steve", LastName = "Wang", Age = 55
            };
            Person p2 = new Person {
                FirstName = "Mike", LastName = "Lee", Age = 37
            };

            Console.WriteLine($"Before swap: <{p1}>, <{p2}>");
            c.Swap <Person>(ref p1, ref p2);
            c.DisplayBaseClass <Person>();
            Console.WriteLine($"After  swap: <{p1}>, <{p2}>");
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("****** Fun with Custom Generic Methods ******\n");
            // Swap 2 ints
            int a = 10, b = 90;

            Console.WriteLine("Before the swap a = {0}, b = {1}", a, b);
            MyGenericMethods.Swap(ref a, ref b);
            Console.WriteLine("After the swap a = {0}, b = {1}", a, b);
            Console.WriteLine();
            // Swap two strings
            string s1 = "Hello", s2 = "World";

            Console.WriteLine("Before swap it reads: {0} {1}", s1, s2);
            MyGenericMethods.Swap(ref s1, ref s2);
            Console.WriteLine("After swap it reads: {0} {1}", s1, s2);
            // Swap two bool
            bool b1 = true, b2 = false;

            Console.WriteLine("Before swap it reads: {0} {1}", b1, b2);
            MyGenericMethods.Swap <bool>(ref b1, ref b2);
            Console.WriteLine("After swap it reads: {0} {1}", b1, b2);
            MyGenericMethods.DisplayBaseClass <int>();
            MyGenericMethods.DisplayBaseClass <string>();
            // Compiler error must supply base class
            // DisplayBaseClass();
            Console.ReadLine();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with custom generic methods ****");
            int i1 = 10;
            int i2 = 5;

            Console.WriteLine("Before swaping: {0} {1}", i1, i2);
            MyGenericMethods.Swap <int>(ref i1, ref i2);
            Console.WriteLine("After swaping: {0} {1}", i1, i2);
            Console.WriteLine();

            string str1 = "Hello";
            string str2 = "there";

            Console.WriteLine("Before swaping: {0} {1}", str1, str2);
            MyGenericMethods.Swap <string>(ref str1, ref str2);
            Console.WriteLine("After swaping: {0} {1}", str1, str2);
            Console.WriteLine();

            MyGenericMethods.DisplayType <bool>();
            MyGenericMethods.DisplayType <string>();
            Console.WriteLine();

            Console.ReadLine();
        }
Exemple #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Custom Generic Methods *****\n");

            // Swap 2 ints.
            int a = 10, b = 90;

            Console.WriteLine("Before swap: {0}, {1}", a, b);
            MyGenericMethods.Swap <int>(ref a, ref b);
            Console.WriteLine("After swap: {0}, {1}", a, b);
            Console.WriteLine();

            // Swap 2 strings.
            string s1 = "Hello", s2 = "There";

            Console.WriteLine("Before swap: {0} {1}", s1, s2);
            MyGenericMethods.Swap <string>(ref s1, ref s2);
            Console.WriteLine("After swap: {0} {1}", s1, s2);
            Console.WriteLine();

            // Compiler will infer System.Boolean.
            bool b1 = true, b2 = false;

            Console.WriteLine("Before swap: {0}, {1}", b1, b2);
            MyGenericMethods.Swap(ref b1, ref b2);
            Console.WriteLine("After swap: {0}, {1}", b1, b2);
            Console.WriteLine();

            // Must supply type parameter if
            // the method does not take params.
            MyGenericMethods.DisplayBaseClass <int>();
            MyGenericMethods.DisplayBaseClass <string>();
            Console.WriteLine();

            // Compiler error! No params? Must supply placeholder!
            // DisplayBaseClass();

            List <int> nums = new List <int> {
                10, 11, 12
            };
            List <string> names = new List <string> {
                "johnny", "steve", "mike"
            };
            List <object> objs = new List <object> {
                "johnny", 24, true
            };

            PrintItemsInList <int>(nums);
            PrintItemsInList <string>(names);
            PrintItemsInList <object>(objs);

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Custom Generic Methods *****\n");

            // Swap 2 ints.
            // no parameters for DisplayBaseClass(), so no signature, so need to provide type T
            MyGenericMethods.DisplayBaseClass <int>();
            int a = 10, b = 90;

            Console.WriteLine("Before swap: {0}, {1}", a, b);
            MyGenericMethods.Swap <int>(ref a, ref b);
            Console.WriteLine("After swap: {0}, {1}", a, b);

            Console.WriteLine();

            // Swap 2 strings.
            MyGenericMethods.DisplayBaseClass <string>();
            string s1 = "Hello", s2 = "There";

            Console.WriteLine("Before swap: {0} {1}", s1, s2);
            MyGenericMethods.Swap <string>(ref s1, ref s2);
            Console.WriteLine("After swap: {0} {1}", s1, s2);
            Console.WriteLine();

            // Compiler will infer System.Boolean from signature
            MyGenericMethods.DisplayBaseClass <bool>();
            bool b1 = true, b2 = false;

            Console.WriteLine("Before swap: {0}, {1}", b1, b2);
            MyGenericMethods.Swap(ref b1, ref b2);
            Console.WriteLine("After swap: {0}, {1}", b1, b2);

            Console.WriteLine();

            Person p1 = new Person()
            {
                FirstName = "John", LastName = "Doe", Age = 21
            };
            Person p2 = new Person()
            {
                FirstName = "Sven", LastName = "Klepsch", Age = 15
            };

            MyGenericMethods.DisplayBaseClass <Person>();
            Console.WriteLine("Before swap: {0}, {1}", p1, p2);
            MyGenericMethods.Swap(ref p1, ref p2);
            Console.WriteLine("After swap: {0}, {1}", p1, p2);
            Console.WriteLine();

            Console.ReadLine();
        }
Exemple #8
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Custom Generic Methods *****\n");

            //  Swap 2 ints.
            int a = 10, b = 90;

            Console.WriteLine("Before swap: {0}, {1}", a, b);
            MyGenericMethods.Swap <int>(ref a, ref b);
            Console.WriteLine("After swap: {0}, {1}", a, b);
            Console.WriteLine();

            //  Swap 2 strings.
            string s1 = "Hello", s2 = "There";

            Console.WriteLine("Before swap: {0}, {1}", s1, s2);
            MyGenericMethods.Swap <string>(ref s1, ref s2);
            Console.WriteLine("After swap: {0}, {1}", s1, s2);
            Console.WriteLine();

            //  Compiler will infer System.Boolean.
            //  Only works if generic method requires arguments.
            //  Should include tpye parameter for readability.
            bool b1 = true, b2 = false;

            Console.WriteLine("Before swap: {0}, {1}", b1, b2);
            MyGenericMethods.Swap(ref b1, ref b2);
            Console.WriteLine("After swap: {0}, {1}", b1, b2);
            Console.WriteLine();

            //  DisplayBaseClass requires no arguments,
            //  so type parameter must be supplied.
            MyGenericMethods.DisplayBaseClass <int>();
            MyGenericMethods.DisplayBaseClass <string>();

            //  Compiler error! No params? Must supply placeholder!
            //DisplayBaseClass();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with custom Generic methods *****\n");

            //  交换两个整数
            int a = 10, b = 90;

            Console.WriteLine("Befero swap:{0},{1}", a, b);
            MyGenericMethods.Swap <int>(ref a, ref b);
            Console.WriteLine("After swap:{0} {1}", a, b);
            Console.WriteLine();

            //  交换两个字符
            string s1 = "Hello", s2 = "There";

            Console.WriteLine("Befero swap:{0} {1}", s1, s2);
            MyGenericMethods.Swap <string>(ref s1, ref s2);
            Console.WriteLine("After swap:{0} {1}", s1, s2);
            Console.WriteLine();

            //  编译器将推断System.Boolen
            bool b1 = true, b2 = false;

            Console.WriteLine("Befero swap:{0} {1}", b1, b2);
            MyGenericMethods.Swap(ref b1, ref b2);
            Console.WriteLine("After swap:{0} {1}", b1, b2);
            Console.WriteLine();

            //  方法没有参数,就必须提供类型参数
            MyGenericMethods.DisplayBaseClass <int>();
            MyGenericMethods.DisplayBaseClass <string>();

            //  编译器错误!没有参数时必须提供占位符
            //  DisplayBaseClass();
            Console.WriteLine();

            Console.ReadLine();
        }
Exemple #10
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Custom Generic Methods *****\n");

            // Swap 2 ints.
            int a = 10, b = 90;

            Console.WriteLine("Before swap: {0}, {1}", a, b);
            MyGenericMethods.Swap <int>(ref a, ref b);
            Console.WriteLine("After swap: {0}, {1}", a, b);
            Console.WriteLine();

            // Swap 2 strings.
            string s1 = "Hello", s2 = "There";

            Console.WriteLine("Before swap: {0} {1}", s1, s2);
            MyGenericMethods.Swap <string>(ref s1, ref s2);
            Console.WriteLine("After swap: {0} {1}", s1, s2);
            Console.WriteLine();

            // Compiler will infer System.Boolean.
            bool b1 = true, b2 = false;

            Console.WriteLine("Before swap: {0}, {1}", b1, b2);
            MyGenericMethods.Swap(ref b1, ref b2);
            Console.WriteLine("After swap: {0}, {1}", b1, b2);
            Console.WriteLine();

            // Must supply type parameter if
            // the method does not take params.
            MyGenericMethods.DisplayBaseClass <int>();
            MyGenericMethods.DisplayBaseClass <string>();

            // Compiler error! No params? Must supply placeholder!
            // DisplayBaseClass();

            Console.ReadLine();
        }
Exemple #11
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with Custom Generic Methods****\n");

            //swap 2 ints
            int a = 10, b = 20;

            Console.WriteLine($"Before swap: {a}:{b}");
            MyGenericMethods.Swap <int>(ref a, ref b);
            Console.WriteLine($"After swap: {a}:{b}");
            Console.WriteLine();

            //swap 2 strings
            string s1 = "Hello", s2 = "There";

            Console.WriteLine($"Before swap: {s1}:{s2}");
            MyGenericMethods.Swap <string>(ref s1, ref s2);
            Console.WriteLine($"After swap: {s1}:{s2}\n");
            MyGenericMethods.DisplayBaseClass <string>();
            MyGenericMethods.DisplayBaseClass <int>();
            MyGenericMethods.DisplayBaseClass <object>();

            Console.ReadKey();
        }
Exemple #12
0
        static void Main(string[] args)
        {
            int    a    = 3;
            int    b    = 4;
            Person per1 = new Person()
            {
                F_Name = "JJ", L_Name = "H", Age = 22
            };
            Person per2 = new Person()
            {
                F_Name = "My", L_Name = "Girl", Age = 21
            };

            MyGenericMethods.Swap <int>(ref a, ref b);
            MyGenericMethods.Swap <Person>(ref per1, ref per2);

            Console.WriteLine("a: {0} b: {1}", a, b);
            Console.WriteLine("per1: {0}", per1);
            Console.WriteLine("per2: {0}", per2);

            MyGenericMethods.DisplayBaseClass <int>();
            MyGenericMethods.DisplayBaseClass <Person>();
            Console.ReadLine();
        }