Tangara G

Tangara G

  • NA
  • 298
  • 90k

can someone explain to me why I can mix string with int here

Nov 8 2016 2:11 AM
Dear experts,
 
I am stumped by the following code :
 
  1. using System;  
  2.   
  3. /// <summary>  
  4. ///     Implements overloaded indexers.  
  5. /// </summary>  
  6. class OvrIndexer  
  7. {  
  8.     private string[] myData;  
  9.     private int         arrSize;  
  10.   
  11.     public OvrIndexer(int size)  
  12.     {  
  13.         arrSize = size;  
  14.         myData = new string[size];  
  15.   
  16.         for (int i=0; i < size; i++)  
  17.         {  
  18.             myData[i] = "empty";  
  19.         }  
  20.     }  
  21.   
  22.     public string this[int pos]  
  23.     {  
  24.         get  
  25.        {  
  26.             return myData[pos];  
  27.         }  
  28.         set  
  29.        {  
  30.             myData[pos] = value;  
  31.         }  
  32.     }  
  33.   
  34.     public string this[string data]  
  35.     {  
  36.         get  
  37.        {  
  38.             int count = 0;  
  39.   
  40.             for (int i=0; i < arrSize; i++)  
  41.             {  
  42.                 if (myData[i] == data)  
  43.                 {  
  44.                     count++;  
  45.                 }  
  46.             }  
  47.             return count.ToString();  
  48.         }  
  49.         set  
  50.        {  
  51.             for (int i=0; i < arrSize; i++)  
  52.             {  
  53.                 if (myData[i] == data)  
  54.                 {  
  55.                     myData[i] = value;  
  56.                 }  
  57.             }  
  58.         }  
  59.     }  
  60.   
  61.     static void Main(string[] args)  
  62.     {  
  63.         int size = 10;  
  64.         OvrIndexer myInd = new OvrIndexer(size);  
  65.   
  66.         myInd[9] = "Some Value";  
  67.         myInd[3] = "Another Value";  
  68.         myInd[5] = "Any Value";  
  69.   
  70.         myInd["empty"] = "no value";  
  71.   
  72.         Console.WriteLine("\nIndexer Output\n");  
  73.   
  74.         for (int i=0; i < size; i++)  
  75.         {  
  76.             Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);  
  77.         }  
  78.   
  79.         Console.WriteLine("\nNumber of \"no value\" entries: {0}", myInd["no value"]);  
  80.     }  
  81. }  
 How come myDate = new String[size] ?
 
and then size can become an integer again in the for-loop that comes next...
 
Just can't see the logic behind.
 
Hope someone can help me out on that .
 
Tks. 
 
 
 
 

Answers (2)