{"id":374,"date":"2014-09-16T13:03:47","date_gmt":"2014-09-16T13:03:47","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=374"},"modified":"2014-09-16T13:03:47","modified_gmt":"2014-09-16T13:03:47","slug":"indexer","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=374","title":{"rendered":"indexer"},"content":{"rendered":"<h1>Indexers<\/h1>\n<pre>Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.<\/pre>\n<pre>class SampleCollection&lt;T&gt;<\/pre>\n<pre>{<\/pre>\n<pre>\u00a0\u00a0\u00a0 \/\/ Declare an array to store the data elements.<\/pre>\n<pre>\u00a0\u00a0\u00a0 private T[] arr = new T[100];<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0 \/\/ Define the indexer, which will allow client code<\/pre>\n<pre>\u00a0\u00a0\u00a0 \/\/ to use [] notation on the class instance itself.<\/pre>\n<pre>\u00a0\u00a0\u00a0 \/\/ (See line 2 of code in Main below.)<\/pre>\n<pre>\u00a0\u00a0\u00a0 public T this[int i]<\/pre>\n<pre>\u00a0\u00a0\u00a0 {<\/pre>\n<pre> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0get<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ This indexer is very simple, and just returns or sets<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ the corresponding element from the internal array.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return arr[i];<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 set<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 arr[i] = value;<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre> \u00a0\u00a0\u00a0}<\/pre>\n<pre>}<\/pre>\n<pre><\/pre>\n<pre>\/\/ This class shows how client code uses the indexer.<\/pre>\n<pre>class Program<\/pre>\n<pre>{<\/pre>\n<pre>\u00a0\u00a0\u00a0 static\u00a0void Main(string[] args)<\/pre>\n<pre>\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Declare an instance of the SampleCollection type.<\/pre>\n<pre>\u00a0\u00a0 SampleCollection&lt;string&gt; stringCollection = new SampleCollection&lt;string&gt;();<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Use [] notation on the type.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 stringCollection[0] = \"Hello, World\";<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.Console.WriteLine(stringCollection[0]);<\/pre>\n<pre>\u00a0\u00a0\u00a0 }<\/pre>\n<pre>}<\/pre>\n<pre>\/\/ Output:<\/pre>\n<pre>\/\/ Hello, World.<\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<h2><span style=\"text-decoration:underline;\">Indexers Overview<\/span><\/h2>\n<ul>\n<li>Indexers enable objects to be indexed in a similar manner to arrays.<\/li>\n<li>A get accessor returns a value. A set accessor assigns a value.<\/li>\n<li>The this keyword is used to define the indexers.<\/li>\n<li>The value keyword is used to define the value being assigned by the set indexer.<\/li>\n<li>Indexers do not have to be indexed by an integer value; it is up to you how to define the specific look-up mechanism.<\/li>\n<li>Indexers can be overloaded.<\/li>\n<li>Indexers can have more than one formal parameter, for example, when accessing a two-dimensional array.<\/li>\n<\/ul>\n<p><b>Yield<\/b><\/p>\n<p>yield is a contextual keyword used in iterator methods in C#. Basically, it has two use cases:<b><\/b><\/p>\n<ul>\n<li>yield return obj; returns the next item in the sequence.<\/li>\n<li>yield break; stops returning sequence elements (this happens automatically if control reaches the end of the iterator method body).<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<pre><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>\u00a0<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Indexers Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters. class SampleCollection&lt;T&gt; { \u00a0\u00a0\u00a0 \/\/ Declare an array to store the data elements. \u00a0\u00a0\u00a0 private T[] arr = new T[100]; \u00a0\u00a0\u00a0 \/\/ Define the indexer, which will allow client code \u00a0\u00a0\u00a0 \/\/ to use [] notation&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=374\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-374","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=374"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/374\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}