예제 #1
0
        static void Main(string[] args)
        {
            #region Info

            /*
             * - Generics allow you to define a class with placeholders for the type of its fields, methods, parameters, etc
             * - Generics replace these placeholders with some specific type at compile time
             * - A generic class can be defined using angle brackets <>
             * - Generics increases the reusability of the code
             * - Generic are type safe. You get compile time errors if you try to use a different type of data than the one specified in the definition
             * - Generic has a performance advantage because it removes the possibilities of boxing and unboxing
             *
             * - When deriving from a generic base class, you must provide a type argument instead of the base-class's generic type parameter
             * - If you want the derived class to be generic then no need to specify type for the generic base class
             */
            #endregion

            Console.WriteLine("\n-------------------- int Generic --------------------");
            MyGenericClass <int> intGeneric = new MyGenericClass <int>(10);
            int val = intGeneric.GenericMethod(200);

            Console.WriteLine("\n-------------------- string Generic --------------------");
            MyGenericClass <string> strGeneric = new MyGenericClass <string>("Hello Generic World");
            string result = strGeneric.GenericMethod("Generic Parameter");
            strGeneric.GenericProperty = "This is a generic property example.";
            Console.WriteLine($"Generic Property: {strGeneric.GenericProperty}");

            Console.ReadLine();
        }
예제 #2
0
        static void Main(string[] args)
        {
            PrintNum(GetNum(2));

            Console.WriteLine();

            MyGenericClass.Print("hello world");
            MyGenericClass.Print(2019);
            MyGenericClass.Print(1.3444f);

            Console.WriteLine();

            // Passed by reference
            string str  = "Hello, World";
            int    num  = 2019;
            float  num2 = 9.81f;

            MyGenericClass.Print(str);
            MyGenericClass.Print(num);
            MyGenericClass.Print(num2);

            Console.WriteLine();

            // An overloaded generic method with three arbitray types as parameters
            MyGenericClass.Print(2, "hello", 'A');
        }
예제 #3
0
        public static void Main()
        {
            MyGenericClass <string> strGenericClass1 = new MyGenericClass <string>("Hello World");

            strGenericClass1.genericMethod("Hello World!");

            MyGenericClass <Student> strGenericClass2 = new MyGenericClass <Student>(new Student());

            //following will throw an exception
            //MyGenericClass<int> intGenericClass = new MyGenericClass<int>(10);
        }
예제 #4
0
        static void Main(string[] args)
        {
            MyGenericClass <int> intGenericClass = new MyGenericClass <int>(10);

            int val = intGenericClass.genericMethod(200);

            add <int> sum = AddNumber;

            Console.WriteLine(sum(10, 20));

            add <string> conct = Concate;

            Console.WriteLine(conct("Hello", "World!!"));
        }
예제 #5
0
        static void Main(string[] args)
        {
            //at the point of instantiating your class the generics helps to replace those place holders with a specified type

            MyGenericClass <int> myGeneric = new MyGenericClass <int>(10);

            myGeneric.GenericMethod(20);
            //Parameter type :System.Int32, value:20
            //Field type: System.Int32, Value10
            MyGenericClass <string> myStringGeneric = new MyGenericClass <string>("This is a generic field");

            myStringGeneric.GenericMethod("This is a generic property");
            //Parameter type :System.String, value:This is a generic property
            //Field type: System.String, ValueThis is a generic field

            Console.ReadLine();
        }
예제 #6
0
파일: Program.cs 프로젝트: lesnerd/test
        static void Main(string[] args)
        {
            var str1 = "David";
            var str2 = "Dav";

            MyGenericClass <string> myGenericClass = new MyGenericClass <string>("David");

            myGenericClass.genericMethod("Lesner");

            Console.WriteLine("Before swapping: str1={0} str2={1}", str1, str2);
            myGenericClass.SwapIfGreater(ref str1, ref str2);
            Console.WriteLine("After swapping: str1={0} str2={1}", str1, str2);

            var dateNow = DateTime.Today.Date.ToString();

            Console.WriteLine("Today date is: {0}", dateNow);
            var myDate = myGenericClass.ConvertStringToDateTime <DateTime>(dateNow);

            Console.ReadKey();
        }
예제 #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("\n-------------------- int Generic --------------------");


            // Here when we are creating an instance of that generic class, we are specifying the type that
            // we want to use, in this example we are saying we want T to be of type INT

            MyGenericClass <int> intGeneric = new MyGenericClass <int>(10);
            int val = intGeneric.GenericMethod(200);

            Console.WriteLine("\n-------------------- string Generic --------------------");

            // You can see here we are using the same class, correct ? But this time we are saying
            // we want T to be of type string, so all of the T's in that class will be replaced with string
            MyGenericClass <string> strGeneric = new MyGenericClass <string>("Hello Generic World");
            string result = strGeneric.GenericMethod("Generic Parameter");

            strGeneric.GenericProperty = "This is a generic property example.";
            Console.WriteLine($"Generic Property: {strGeneric.GenericProperty}");

            Console.ReadLine();
        }
예제 #8
0
 static void Main(string[] args)
 {
     MyGenericClass <int> myGenericClass = new MyGenericClass <int>();
 }