C# Generics

Generics in C# allows defining the specification of the data type of programming elements in a class or a method until it is actually used in the program. Specification for the class or method can be written with substitute parameters for the data type.

Following are some important features of Generic:

  1. Generics in C# helps to maximize the code reuse, performance and type safety.
  2. Your own generic interfaces, classes, methods, events, and delegates can also be created in C#.
  3. It can create new Generic classes constrained to enable access to methods on some particular data types.
  4. Also can have the information about the types used in a generic data type at run-time.
  5. Generic collection classes can also be created. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace.
  6. All these generic collection classes can be used instead of the collection classes in the System.Collections namespace

To better understand the concept of generic review this example:

using System;
using System.Collections.Generic;

namespace CsharpGeneric {
   public class CGenericArray<G> {
      private G[] array;
      
      public CGenericArray(int size) {
         array = new G[size + 1];
      }
      public G getItem(int index) {
         return array[index];
      }
      public void setItem(int index, G value) {
         array[index] = value;
      }
   }
  public class Tester {
    public  static void Main(string[] args) {
         
         //declaring an int array
         CGenericArray<int> intArray = new CGenericArray<int>(5);
         
         //setting values
         for (int c = 0; c < 3; c++) {
            intArray.setItem(c, c*3);
         }
         
         //retrieving the values
         for (int c = 0; c < 3; c++) {
            Console.Write(intArray.getItem(c) + " ");
         }
         
         Console.WriteLine();
         
         //declaring a character array
         CGenericArray<char> charArray = new CGenericArray<char>(5);
         
         //setting values
         for (int c = 0; c < 3; c++) {
            charArray.setItem(c, (char)(c+97));
         }
         
         //retrieving the values
         for (int c = 0; c< 3; c++) {
            Console.Write(charArray.getItem(c) + " ");
         }
         Console.WriteLine();
         
	}
   }
}

Generic Delegates:

The Generic Delegates in C# are considered as part of .NET Framework 3.5. It’s considered as it doesn’t require the definition of the delegate instance in order to invoke the methods.

Types of Generic Delegates:

Given below are the types for generic delegates. C# provides three built-in generic delegates,

  1. Func
  2. Action
  3. Predicate

Generic delegates can be defined with type parameters.

delegate T NumberChanger<T>(T n);
using System;
using System.Collections.Generic;

delegate G NumberChanger<G>(G n);
namespace GenericDelegate {
 public  class TestDelegate {
   public   static int number = 47;
      
      public static int AddNumber(int a) {
         number += a;
         return number;
      }
      public static int Multnumber(int b) {
         number *= b;
         return number;
      }
      public static int getnumber() {
         return number;
      }
    public  static void Main(string[] args) {
         //create delegate instances
         NumberChanger<int> nc1 = new NumberChanger<int>(AddNumber);
         NumberChanger<int> nc2 = new NumberChanger<int>(Multnumber);
         
         //calling the methods using the delegate objects
         nc1(25);
         Console.WriteLine("Value of a: {0}", getnumber());
         
         nc2(5);
         Console.WriteLine("Value of b: {0}", getnumber());
         
      }
   }
}