Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Learning System.Collections.Generic

using System;
using System.Collections.Generic;

namespace GenericApplication {
   public class MyGenericArray<T> {
      private T[] array;
      
      public MyGenericArray(int size) 
      {
         array = new T[size];
         Console.Write(array.Length + " ");
      }
      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)
      {
         
         //declaring an int array
         MyGenericArray<int> intArray = new MyGenericArray<int>(5);
         
         //setting values
         for (int c = 0; c < 5; c++)
         {
            intArray.setItem(c, c*6);
         }
         
         //retrieving the values
         for (int c = 0; c < 5; c++) {
            Console.Write(intArray.getItem(c) + " ");
         }
         
         //declaring an string array
         MyGenericArray<string> stringArray = new MyGenericArray<string>(5);
         
         //setting values
            stringArray.setItem(0, "A");
            stringArray.setItem(1, "B");
            stringArray.setItem(2, "C");
            stringArray.setItem(3, "D");
            stringArray.setItem(4, "E");

         //retrieving the values
         for (int c = 0; c < 5; c++) {
            Console.Write(stringArray.getItem(c) + " ");
         }
         
         Console.WriteLine();
         
         Console.ReadKey();
      }
   }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.