1.50. C# indexer

发布时间 :2023-10-12 23:00:07 UTC      

The Indexer allows an object to be accessed in the same way as an array using a subscript.

When you define an indexer for a class, the class behaves like a virtual array. You can use arrays to access operators [ ] to access members of this class.

1.50.1. Grammar #

The syntax of an one-dimensional indexer is as follows:

element-type this[int index]
{
   // get accessor
   get
   {
      // returns the value specified by index
   }
   // set accessor
   set
   {
      // set the value specified by index
   }
}

1.50.2. The purpose of the indexer #

The declaration of the indexer’s behavior is somewhat similar to a property.Like property, you can use the get and set accessor to define theindexer. However, the property returns or sets a specific data member, and the indexer returns or sets a specific value for the object instance. In other words, it divides the instance data into smaller parts, indexes each part, and gets or sets each part.

Defining an property includes providing the name of the attribute. Indexers are defined without a name, but with this keyword, which points to the object instance. The following example demonstrates this concept:

Example #

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      public IndexedNames()
      {
         for (int i = 0; i < size; i++)
         namelist[i] = "N. A.";
      }
      public string this[int index]
      {
         get
         {
            string tmp;
            if( index >= 0 && index <= size-1 )
            {
               tmp = namelist[index];
            }
            else
            {
               tmp = "";
            }
            return ( tmp );
         }
         set
         {
            if( index >= 0 && index <= size-1 )
            {
               namelist[index] = value;
            }
         }
      }
      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         for ( int i = 0; i < IndexedNames.size; i++ )
         {
            Console.WriteLine(names[i]);
         }
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.

1.50.3. Overloaded indexer #

The indexer can be overloaded. Indexers can also be declared with multiple parameters, and each parameter can be of a different type. There isno need for indexers to be integer. C# allows indexers to be of other types, such as string types.

The following example demonstrates the overloaded indexer:

Example #

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      public IndexedNames()
      {
         for (int i = 0; i < size; i++)
         {
          namelist[i] = "N. A.";
         }
      }
      public string this[int index]
      {
         get
         {
            string tmp;
            if( index >= 0 && index <= size-1 )
            {
               tmp = namelist[index];
            }
            else
            {
               tmp = "";
            }
            return ( tmp );
         }
         set
         {
            if( index >= 0 && index <= size-1 )
            {
               namelist[index] = value;
            }
         }
      }
      public int this[string name]
      {
         get
         {
            int index = 0;
            while(index < size)
            {
               if (namelist[index] == name)
               {
                return index;
               }
               index++;
            }
            return index;
         }
      }
      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         // Using the first indexer with an int parameter
         for (int i = 0; i < IndexedNames.size; i++)
         {
            Console.WriteLine(names[i]);
         }
         // Using a second indexer with a string parameter
         Console.WriteLine(names["Nuha"]);
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.