C# GENERICS

Posted on Updated on

C# GENERICS

Generics helps you to specify class or procedures that can accept any type of data. They are like ready made recipes that can be used to create any type of classes or procedures based upon the type you passed to it.

In this case you just write the classes or methods that can accept any type of data or parameters, when compiler comes across such methods or classes it spawns the program in according to that data type you passed to it.

For example:

void function (int, int);

is a type of function that only accept data of integer type? This function have a limitation that it only accept parameters of integer type not of float type. You can’t use the same function for float type numbers.

How to do Generics Programming in C#

For this one must have to use System. Collections. Generic class for creating such Generic methods or classes.

Using System;

using System.Collections.Generic; //Class used to show the concept of Generic Programming

namespace Generic_Programming //Name space is the name of the program defining the scope

of the program

{

class Program

{

static void Main(string[] args)

{

//declaring an int array

MyGenericArray<int> intArray = new MyGenericArray<int>(5); //Now declaring

generic class

//setting values

intArray.setItem(1, 1 * 5);

//retrieving the values

Console.Write(intArray.getItem(1) + ” “);

Console.WriteLine();

//declaring a character array

MyGenericArray<char> charArray = new MyGenericArray<char>(5);

//setting values

charArray.setItem(1, (char)(1 + 97));

//retrieving the values

Console.Write(charArray.getItem(1) + ” “);

Console.WriteLine();

Console.ReadKey();

}

}

public class MyGenericArray<T> //Creating a Generic Class of T type variables

{

private T[] array; // Creating an Array of T type variables

public MyGenericArray(int size) //Using a constructor to intialize array with the

size provided by the user size is integer as array size is not in float

{

array = new T[size + 1]; //If user enter size=2 then array automatically got

size of 2+1 that is thrree

}

public T getItem(int index) //Created a generic function having return type T

{

return array[index]; //Returning array value which is being asked by the

user

}

public void setItem(int index, T value) // function used to provide value to an

array

{

array[index] = value; //providing value to the array index

}

}

}

Output: 5

B

This program created a generic class named mygeneric array consist of one constructor used to initialize an array, getitem used to provide index value to the generic array, and setitem used to provide index value and index to the array. In main part we have shown that we can create character and integer type array by simply passing the value of intentional data type. If we passed integer type value then it implicitly converted into integer type array and if we pass float type value to the array it automatically converted to float value.

How to use Generic Methods:

In the above program we have used a generic class, in which created different arrays using generic classes. Now to elucidate the notion of Generic methods, following program is constructed:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

//Program to show the concept of Generic methohds in C#

namespace Generic_Methods

{

class Program

{

static void swapping<T>(ref T x, ref T y) //Method declared which have T type

parameters

{

T temp; //Variable of Y yupe declared

temp = x;

x = y;

x = temp;

}

static void Main(string[] args)

{

int a, b;

char c, d;

a = 10;

b = 20;

c = ‘I’;

d = ‘V’;

Console.WriteLine(“Values before swapping \n”);

//display values before swap:

Console.WriteLine(“a = {0}, b = {1}”, a, b);

Console.WriteLine(“c = {0}, d = {1}”, c, d);

//call swap

swapping<int>(ref a, ref b); //calling template method by passing integer

swapping<char>(ref c, ref d); //calling template method for char values

Console.WriteLine(“Values after swapping \n\n”);

//display values after swap:

Console.WriteLine(“a = {0}, b = {1}”, a, b);

Console.WriteLine(“c = {0}, d = {1}”, c, d);

Console.ReadKey();

}

}

}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s