Latest web development tutorials

C # Generics (Generic)

Generic (Generic) allows you to delay the preparation of the data type of the class or method programming elements of the specification, until it's time to actually use the program.In other words, it allows you to write a generic class or method that can work with any data type.

You can write the specification class or method by substituting the parameter data types. When the function is called a constructor or method compiler encounters a class that will generate code to handle the data type specified. The following simple example will help you understand this concept:

using System;
using System.Collections.Generic;

namespace GenericApplication
{
    public class MyGenericArray <T>
    {
        private T [] array;
        public MyGenericArray (int size)
        {
            array = new T [size + 1];
        }
        public T getItem (int index)
        {
            return array [index];
        }
        public void setItem (int index, T value)
        {
            array [index] = value;
        }
    }
           
    class Tester
    {
        static void Main (string [] args)
        {
            // Declare an integer array MyGenericArray <int> intArray = new MyGenericArray <int> (5);
            // Set the value for (int c = 0; c <5; c ++)
            {
                intArray.setItem (c, c * 5);
            }
            // Get value for (int c = 0; c <5; c ++)
            {
                Console.Write (intArray.getItem (c) + "");
            }
            Console.WriteLine ();
            // Declare a character array MyGenericArray <char> charArray = new MyGenericArray <char> (5);
            // Set the value for (int c = 0; c <5; c ++)
            {
                charArray.setItem (c, (char) (c + 97));
            }
            // Get value for (int c = 0; c <5; c ++)
            {
                Console.Write (charArray.getItem (c) + "");
            }
            Console.WriteLine ();
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

05101520
abcde

Generic (Generic) features

Use of generics is a program to enhance technical capabilities, specifically in the following aspects:

  • It helps you maximize code reuse, type of protection of safety and improving performance.
  • You can create a generic collection classes. .NET Framework class library contains several new generic collection classes in theSystem.Collections.Genericnamespace. You can use these generic collection classes insteadSystem.Collectionscollection classes.
  • You can create your own generic interfaces, generic classes, generic method generic events and generic delegate.
  • You can constrain the generic class to access specific data types.
  • About generic data types used in the type of information can be obtained at run time by using reflection.

Generic (Generic) method

In the example above, we have used the generic class, we can declare a generic method type parameters. The following program illustrates this concept:

using System;
using System.Collections.Generic;

namespace GenericMethodAppl
{
    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';

            // Display before the exchange value 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 swap
            Swap <int> (ref a, ref b);
            Swap <char> (ref c, ref d);

            // After an exchange of display value 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 ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Int values ​​before calling swap:
a = 10, b = 20
Char values ​​before calling swap:
c = I, d = V
Int values ​​after calling swap:
a = 20, b = 10
Char values ​​after calling swap:
c = V, d = I

Generic (Generic) commissioned

You can define through generic delegate type parameters. E.g:

delegate T NumberChanger <T> (T n);

The following example demonstrates the use of commission:

using System;
using System.Collections.Generic;

delegate T NumberChanger <T> (T n);
namespace GenericDelegateAppl
{
    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 a delegate instance NumberChanger <int> nc1 = new NumberChanger <int> (AddNum);
            NumberChanger <int> nc2 = new NumberChanger <int> (MultNum);
            // Call the method using a delegate object nc1 (25);
            Console.WriteLine ( "Value of Num: {0}", getNum ());
            nc2 (5);
            Console.WriteLine ( "Value of Num: {0}", getNum ());
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Value of Num: 35
Value of Num: 175