Create your own generic class - CSharp Custom Type

CSharp examples for Custom Type:Generics

Description

Create your own generic class

using System;
using System.Collections.Generic;

   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(string[] args) {
         MyGenericArray<int> intArray = new MyGenericArray<int>(5);

         for (int c = 0; c < 5; c++) {
            intArray.setItem(c, c*5);
         }

         for (int c = 0; c < 5; c++) {
            Console.Write(intArray.getItem(c) + " ");
         }
         MyGenericArray<char> charArray = new MyGenericArray<char>(5);
         for (int c = 0; c < 5; c++) {
            charArray.setItem(c, (char)(c+97));
         }
         for (int c = 0; c< 5; c++) {
            Console.Write(charArray.getItem(c) + " ");
         }
      }
   }

Related Tutorials