예제 #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)
        {
            //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();
        }
예제 #3
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();
        }