Created
February 25, 2022 11:29
-
-
Save charanhu/b9c2d40249764d6f51769182d8b49425 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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