What is Generics in C#

Generics enable you to delay the specification of the info form of programming parts during a category or a way, till it’s really utilized in the program. In different words, generics enable you to put in writing a category or technique that may work with any information sort.
You write the specifications for the category or the tactic, with substitute parameters for information sorts. once the compiler encounters a builder for the category or a call for the tactic, it generates code to handle the particular information sort. a straightforward example would facilitate understanding the concept:

using System;
using System.Collections.Generic;

namespace GenericClassDemo
{
   public class GenericArray<T>
   {
      private T[] array;
      public GenericArray(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)
      {
         
         //declaring an int array
         GenericArray<int> intArray = new GenericArray<int>(5);
         
         //setting values
         for (int c = 0; c < 5; c++)
         {
            intArray.setItem(c, c*5);
         }
         
         //retrieving the values
         for (int c = 0; c < 5; c++)
         {
            Console.Write(intArray.getItem(c) + " ");
         }
         
         Console.WriteLine();
         
         //declaring a character array
         GenericArray<char> charArray = new GenericArray<char>(5);
         
         //setting values
         for (int c = 0; c < 5; c++)
         {
            charArray.setItem(c, (char)(c+97));
         }
         
         //retrieving the values
         for (int c = 0; c< 5; c++)
         {
            Console.Write(charArray.getItem(c) + " ");
         }
         Console.WriteLine();
         
         Console.ReadKey();
      }
   }
}

When the on top of code is compiled and dead, it produces the subsequent result:

0 5 10 15 20
a b c d e

Features of Generics

Generics could be a technique that enriches your programs within the following ways:
• It helps you to maximise code use, sort safety, and performance.
• You will produce generic assortment categories. The .NET Framework category library contains many new generic assortment categories within the System.Collections.Generic namespace. you will use these generic assortment categories rather than the gathering categories within the System.Collections namespace.
• You will produce your own generic interfaces, classes, methods, events, and delegates.
• You might produce generic categories forced to alter access to strategies on specific information sorts.
• You might get info on {the sorts|the kinds|the categories} utilized in a generic information type at run-time by means that of reflection.

Generic Methods

In the previous example, we’ve used a generic class; we are able to declare a generic technique with a kind parameter. the subsequent program illustrates the concept:

using System;
using System.Collections.Generic;

namespace GenericMethodDemo
{
   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 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 swap
         Swap<int>(ref a, ref b);
         Swap<char>(ref c, ref d);
         
         //display 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();
      }
   }
}

When the on top of code is compiled and dead, it produces the subsequent result:

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 Delegates

You can outline a generic delegate with sort parameters. For example:

delegate T NumberChanger<T>(T n);

The following example shows use of this delegate:

using System;
using System.Collections.Generic;

delegate T NumberChanger<T>(T n);
namespace GenericDelegateDemo
{
   class DelegateClass
   {
      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();
      }
   }
}

When the on top of code is compiled and dead, it produces the subsequent result:

Value of Num: 35
Value of Num: 175

Leave a Reply

Your email address will not be published. Required fields are marked *