{"id":315,"date":"2020-10-15T11:27:20","date_gmt":"2020-10-15T05:57:20","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=315"},"modified":"2020-10-15T11:28:52","modified_gmt":"2020-10-15T05:58:52","slug":"indexers","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2020\/10\/15\/indexers\/","title":{"rendered":"Indexers"},"content":{"rendered":"\n<p>An\u00a0<strong>indexer<\/strong>\u00a0allows an object to be indexed such as an array. When you define an indexer for a class, this class behaves similar to a\u00a0<strong>virtual array<\/strong>. You can then access the instance of this class using the array access operator ([ ]).<\/p>\n\n\n\n<h2>Use of Indexers<\/h2>\n\n\n\n<p>Declaration of behavior of an indexer is to some extent similar to a property. similar to the properties, you use&nbsp;<strong>get<\/strong>&nbsp;and&nbsp;<strong>set<\/strong>&nbsp;accessors for defining an indexer. However, properties return or set a specific data member, whereas indexers returns or sets a particular value from the object instance. In other words, it breaks the instance data into smaller parts and indexes each part, gets or sets each part.<\/p>\n\n\n\n<p>Defining a property involves providing a property name. Indexers are not defined with names, but with the\u00a0<strong>this<\/strong>\u00a0keyword, which refers to the object instance. The following example demonstrates the concept \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\r\n\r\nnamespace IndexerApplication {\r\n   \r\n   class IndexedNames {\r\n      private string&#91;] namelist = new string&#91;size];\r\n      static public int size = 10;\r\n      \r\n      public IndexedNames() {\r\n         for (int i = 0; i &lt; size; i++)\r\n         namelist&#91;i] = \"N. A.\";\r\n      }\r\n      public string this&#91;int index] {\r\n         get {\r\n            string tmp;\r\n         \r\n            if( index >= 0 &amp;&amp; index &lt;= size-1 ) {\r\n               tmp = namelist&#91;index];\r\n            } else {\r\n               tmp = \"\";\r\n            }\r\n            \r\n            return ( tmp );\r\n         }\r\n         set {\r\n            if( index >= 0 &amp;&amp; index &lt;= size-1 ) {\r\n               namelist&#91;index] = value;\r\n            }\r\n         }\r\n      }\r\n      static void Main(string&#91;] args) {\r\n         IndexedNames names = new IndexedNames();\r\n         names&#91;0] = \"Zara\";\r\n         names&#91;1] = \"Riz\";\r\n         names&#91;2] = \"Nuha\";\r\n         names&#91;3] = \"Asif\";\r\n         names&#91;4] = \"Davinder\";\r\n         names&#91;5] = \"Sunil\";\r\n         names&#91;6] = \"Rubic\";\r\n         \r\n         for ( int i = 0; i &lt; IndexedNames.size; i++ ) {\r\n            Console.WriteLine(names&#91;i]);\r\n         }\r\n         Console.ReadKey();\r\n      }\r\n   }\r\n}<\/code><\/pre>\n\n\n\n<h2>Overloaded Indexers<\/h2>\n\n\n\n<p>Indexers can be overloaded. Indexers can also be declared with multiple parameters and each parameter may be a different type. It is not necessary that the indexes have to be integers. C# allows indexes to be of other types, for example, a string.<\/p>\n\n\n\n<p>The following example demonstrates overloaded indexers \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\r\n\r\nnamespace IndexerApplication {\r\n   class IndexedNames {\r\n      private string&#91;] namelist = new string&#91;size];\r\n      static public int size = 10;\r\n      \r\n      public IndexedNames() {\r\n         for (int i = 0; i &lt; size; i++) {\r\n            namelist&#91;i] = \"N. A.\";\r\n         }\r\n      }\r\n      public string this&#91;int index] {\r\n         get {\r\n            string tmp;\r\n            \r\n            if( index >= 0 &amp;&amp; index &lt;= size-1 ) {\r\n               tmp = namelist&#91;index];\r\n            } else {\r\n               tmp = \"\";\r\n            }\r\n            \r\n            return ( tmp );\r\n         }\r\n         set {\r\n            if( index >= 0 &amp;&amp; index &lt;= size-1 ) {\r\n               namelist&#91;index] = value;\r\n            }\r\n         }\r\n      }\r\n      \r\n      public int this&#91;string name] {\r\n         get {\r\n            int index = 0;\r\n            \r\n            while(index &lt; size) {\r\n               if (namelist&#91;index] == name) {\r\n                return index;\r\n               }\r\n               index++;\r\n            }\r\n            return index;\r\n         }\r\n      }\r\n\r\n      static void Main(string&#91;] args) {\r\n         IndexedNames names = new IndexedNames();\r\n         names&#91;0] = \"Zara\";\r\n         names&#91;1] = \"Riz\";\r\n         names&#91;2] = \"Nuha\";\r\n         names&#91;3] = \"Asif\";\r\n         names&#91;4] = \"Davinder\";\r\n         names&#91;5] = \"Sunil\";\r\n         names&#91;6] = \"Rubic\";\r\n         \r\n         \/\/using the first indexer with int parameter\r\n         for (int i = 0; i &lt; IndexedNames.size; i++) {\r\n            Console.WriteLine(names&#91;i]);\r\n         }\r\n         \r\n         \/\/using the second indexer with the string parameter\r\n         Console.WriteLine(names&#91;\"Nuha\"]);\r\n         Console.ReadKey();\r\n      }\r\n   }\r\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An\u00a0indexer\u00a0allows an object to be indexed such as an array. When you define an indexer for a class, this class behaves similar to a\u00a0virtual array. You can then access the instance of this class using&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,4,2],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/315"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":2,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"predecessor-version":[{"id":317,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions\/317"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}