Generics Example in C#

using System;

namespace Generics
{

public class MyGeneric<T>
{   //type of the class is ommited
private T[] array;
public MyGeneric(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 Program
{
static void Main(string[] args)
{
//declare double array
MyGeneric<double> doubleArray = new MyGeneric<double>(5);

//set values
double x;
for (int c = 0; c < 5; c++)
{
x = c * 10;
doubleArray.setItem(c, Convert.ToDouble(x));
}

//get values
for (int a = 0; a < 5; a++)
{
Console.WriteLine(doubleArray.getItem(a));
}

Console.ReadLine();
}

}
}

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s