Generics in C#

Total Post:36

Points:252
C#
 306  View(s)
Ratings:
Rate this:

Please describe in brief with an example.

  1. Post:58

    Points:408
    Re: Generics in C#

    Generics in C#

    Here, Today, I would like to share the basics of generics. The Generic in C# is a concept that allows us to define classes and methods with the placeholder. The C# compiler replaces these placeholders with specified type at compile time. That is the concept of generics is used to create general-purpose classes and methods. When defining a generic class, we must use angle <> brackets. The angle brackets are used to declare a class or method as a generic type. In the following example, we are creating a generic class that can be used to deal with any type of data. Generics are so important because of that, we acquire the ability to create such classes, structures, interfaces, methods, and delegates, which work in type-safe for different types of data. The Generics(class or methods) in C# and .NET procedure many of the benefits of strongly-typed collections as well as provide a higher quality of and a performance boost for the code. The Generics are very similar to C++ templates but having a slight difference in such a way that the source code of C++ templates is required when the templates are instantiated with the specific type and .NET Generics are not limited to classes only. 

    Example - 1

    using System;
    using System.Collections.Generic;
    using System.Linq; using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Swap<T>(ref T lhs, ref T rhs)
            {
                T temp;
                temp = lhs;
                lhs = rhs;             rhs = temp;
            }
            static void Main(string[] args)
            {
                int a, b;
                char c, d;
                a = 10;
                b = 20;
                c = 'I';
                d = 'V';

                // values before swap:
                Console.WriteLine("Int values before calling swap:");
                Console.WriteLine("a = {0}, b = {1}", a, b);
                Console.WriteLine("Char values before calling swap:");
                Console.WriteLine("c = {0}, d = {1}", c, d);
                //call the swap
                Swap<int>(ref a, ref b);
                Swap<char>(ref c, ref d);
                // values after swap:
                Console.WriteLine("Int values after calling swap:");
                Console.WriteLine("a = {0}, b = {1}", a, b);
                Console.WriteLine("Char values after calling swap:");
                Console.WriteLine("c = {0}, d = {1}", c, d);
                Console.ReadKey();
            }
        }
    } Generics in C#

    Example - 2.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    delegate T NumberChanger<T>(T n);
    namespace ConsoleApplication3
    {
        class TestDelegate
        {
            static int num = 10;
            public static int AddNum(int p)
            {
                num += p;
                return num;
            }
            public static int MultNum(int q)
            {
                num *= q;
                return num;
            }
            public static int getNum()
            {
                return num;
            }
            static void Main(string[] args)
            {
                //create delegate instances
                NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
                NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);
                //calling the methods using the delegate objects
                nc1(25);
                Console.WriteLine("Value of Num: {0}", getNum());
                nc2(5);
                Console.WriteLine("Value of Num: {0}", getNum());
                Console.ReadKey();
            }
        }
    } Generics in C#


      Modified On Dec-14-2018 06:53:04 AM

Answer