示例#1
0
        static void Main(string[] args)

        {
            // Instantiate Generic Class, string is the type argument

            GenericClass <string> gclass = new GenericClass <string>();

            gclass.msg = "Generics Example";

            gclass.genericMethod("Navya", "Hyderabad");

            Console.ReadLine();
        }
示例#2
0
        static void Main(string[] args)
        {
            //GenericClass<int> integerGenericClass = new GenericClass<int>(10);
            //int val = integerGenericClass.genericMethod(200);


            //  At the time of instantiation, we can use any type as per our requirement.
            //  If we want to use a string type, then we need to instantiate the class as shown below

            GenericClass <string> stringGenericClass = new GenericClass <string>("Hello Generic World");

            stringGenericClass.genericProperty = "This is a generic property example.";
            string result = stringGenericClass.genericMethod("Generic Parameter");
        }
示例#3
0
        static void Main(string[] args)
        {
            // Generic method in a non-generic class
            int val1 = 5;
            int val2 = 10;

            Console.WriteLine(Calculator.AreEqual <int>(val1, val2));
            Console.WriteLine(Calculator.AreEqual <int>(val1, 5));

            // Generic class with generic methods
            GenericClass <int> gen1 = new GenericClass <int>(3);

            gen1.genericProperty = 6;
            Console.WriteLine("Generic property is {0}", gen1.genericProperty);

            gen1.genericMethod(66);

            GenericClass <string> gen2 = new GenericClass <string>("Hello");

            gen2.genericProperty = "Greetings";
            Console.WriteLine("Generic property is {0}", gen2.genericProperty);

            gen2.genericMethod("Salutations");

            // Generic list
            List <int> integerList = new List <int>();

            Console.WriteLine("Initial Capacity: " + integerList.Capacity);
            integerList.Add(10);
            Console.WriteLine("Capacity after adding first item: " + integerList.Capacity);
            integerList.Add(20);
            integerList.Add(30);
            integerList.Add(40);
            Console.WriteLine("Capacity after adding fourth item: " + integerList.Capacity);
            integerList.Add(60);
            Console.WriteLine("Capacity after adding 5th element: " + integerList.Capacity);
            //Printing the List items using for loop
            Console.WriteLine("Printing the List items using for loop:");
            for (int i = 0; i < integerList.Count; i++)
            {
                Console.Write(integerList[i] + "  ");
            }
            Console.WriteLine();

            integerList.Remove(20);
            Console.WriteLine("Capacity after removing an element: " + integerList.Capacity);

            integerList.Insert(2, 25);
            Console.WriteLine("List items after inserting the value 25 in the index 2");
            foreach (int item in integerList)
            {
                Console.Write(item + "  ");
            }
            Console.WriteLine();

            List <int> newIntegerList = new List <int>(integerList);

            Console.WriteLine("Initial capacity of new list collection:" + newIntegerList.Capacity);
            // Printing the values of the new list collection using foreach loop
            Console.WriteLine("Printing the new List items which is created from the old list");
            foreach (int item in newIntegerList)
            {
                Console.Write(item + "  ");
            }
        }