Advertisement
RupeshAcharya60

Indexer with C#

Jun 15th, 2022
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. //Implementation of Indexer C#
  2.  
  3.  
  4.     class State
  5.     {
  6.         //class member
  7.         private string name;
  8.         private string[] places = new string[3];
  9.        
  10.         //setting the properties
  11.         public string Name
  12.         {
  13.             get { return name; }
  14.             set { name = value; }
  15.         }
  16.  
  17.         public string this[int index]
  18.         {
  19.             get { return places[index]; }
  20.             set { places[index] = value; }
  21.         }
  22.  
  23.         public void DisplayAllPlaces()
  24.         {
  25.             Console.WriteLine("The Places of " + name + " State:");
  26.             foreach(string place in places)
  27.             {
  28.                 Console.WriteLine(place);
  29.             }
  30.         }
  31.     }
  32.     internal class Program
  33.     {
  34.         static void Main(string[] args)
  35.         {
  36.  
  37.             State s = new State();
  38.             s.Name = "Bagmati";
  39.             s[0] = "BalKumari";
  40.             s[1] = "Kalanki";
  41.             s[2] = "Imadol";
  42.             s.DisplayAllPlaces();
  43.  
  44.             Console.ReadKey();
  45.         }
  46.  
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement