Skip to content

Instantly share code, notes, and snippets.

@charanhu
Created February 25, 2022 11:29
Show Gist options
  • Save charanhu/b9c2d40249764d6f51769182d8b49425 to your computer and use it in GitHub Desktop.
Save charanhu/b9c2d40249764d6f51769182d8b49425 to your computer and use it in GitHub Desktop.
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()
{
//declaring an int array
MyGenericArray<int> intArray = new MyGenericArray<int>(5);
Console.WriteLine("Generic");
Console.WriteLine();
//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
MyGenericArray<char> charArray = new MyGenericArray<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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment