Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Indexer


May 11, 2021 C#


Table of contents


Indexer

The Indexer allows an object to be indexed like an array. W hen you define an indexer for a class, it behaves like a virtual array. You can use the array access operator , , to access instances of the class.

Grammar

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

element-type this[int index] 
{
   // get 访问器
   get 
   {
      // 返回 index 指定的值
   }

   // set 访问器
   set 
   {
      // 设置 index 指定的值 
   }
}

The purpose of the indexer

The indexer behaves in a way that is similar to a property. L ike property, you can use get and set accessors to define indexers. H owever, the property returns or sets a specific data member, while the indexer returns or sets a specific value for the object instance. In other words, it divides the instance data into smaller parts and indexes each part, getting or setting each part.

Defining a property includes providing the property name. T he indexer is defined without a name, but with the this keyword, which points to an object instance. The following example demonstrates this concept:

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.

Overload Indexer

Indexers can be overloaded. I ndexers can also declare with multiple parameters, each of which can be of a different type. T here is no need to make the indexer integer. The indexer can be of other types, such as string types.

The following example demonstrates an overloaded indexer:

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";// 使用带有 int 参数的第一个索引器
        	for (int i = 0; i < IndexedNames.size; i++)
        	{
        		Console.WriteLine(names[i]);
        	}// 使用带有 string 参数的第二个索引器
        	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