{"id":210,"date":"2017-05-06T12:49:15","date_gmt":"2017-05-06T12:49:15","guid":{"rendered":"http:\/\/www.dimlucas.com\/?p=210"},"modified":"2017-05-06T13:01:31","modified_gmt":"2017-05-06T13:01:31","slug":"overloading-the-indexer-operator-in-c","status":"publish","type":"post","link":"https:\/\/www.dimlucas.com\/index.php\/2017\/05\/06\/overloading-the-indexer-operator-in-c\/","title":{"rendered":"Overloading the indexer operator in C#"},"content":{"rendered":"<p>There&#8217;s a feature in C# that&#8217;s very useful for some use cases, yet many people seem to overlook it. I&#8217;m talking about the indexer operator. What this operator does is, it makes it possible to access elements of an enumerable data structure that your class makes use of, with the familiar [] operator. <\/p>\n<p>Let&#8217;s say we have a class named Car:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/dimlucas\/07682bae9c6f4189a57197107b3087d3.js\"><\/script><\/p>\n<p>Now let&#8217;s say that we have multiple cars, multiple instances of the class Car and that we want to organize them in a collection. What should we use? A plain old Generic List you might say and you would be correct, but what happens if we want to augment that list with more operations, more car-specific operations. Perhaps we want to clean the cars, or repair them. All that logic can nicely go inside a class named Garage:<\/p>\n<p>As you can see we still use a List<T> to store the cars, but we&#8217;re using the delegate pattern. A generic list has a bunch of methods but we only need three of them, the Add(), ElementAt() and Remove() methods. Oh and we also need the Count property so that we know how many cars we have.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/dimlucas\/5063e215d65a7221127405aa2097e87f.js\"><\/script><\/p>\n<p>This is an initial draft of our garage class showing the use of the delegate pattern. We can also include other stuff to make the class resemble real-life garage even more<\/p>\n<p><script src=\"https:\/\/gist.github.com\/dimlucas\/8d39fd4466b33f2d834662251a9ed92c.js\"><\/script><\/p>\n<p>The addition of the Clean() and Repair() methods improves our Garage implementation but there&#8217;s something missing. Every time we want to access a specific car by index we are forced to use the Get() method. It&#8217;s counter-intuitive. Here&#8217;s where the powerful indexer operator comes into play. After we&#8217;ve implemented an indexer operator in our collection class we will be able to access car elements with array-like syntax, like this:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/dimlucas\/33ce017d46790e16ecce1851cf5da828.js\"><\/script><\/p>\n<p>Let&#8217;s see how simple it is to do that<\/p>\n<p><script src=\"https:\/\/gist.github.com\/dimlucas\/b0437066f50af9c2ec6d64f56f88202e.js\"><\/script><\/p>\n<p>So basically, you override the indexer operator like a property, with a getter and a setter. In this particular example we don&#8217;t want to be able to manipulate the contents of the garage so we don&#8217;t need a setter. But using a setter is a totally legitimate operation and one you will probably find pretty helpful once you start using it. That&#8217;s how easy it is \ud83d\ude42<\/p>\n<p>Check the whole project on <a href=\"https:\/\/github.com\/dimlucas\/CSharpBlog\"> this GitHub repo<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a feature in C# that&#8217;s very useful for some use cases, yet many people seem to overlook it. I&#8217;m talking about the indexer operator. What this operator does is, it makes it possible to access elements of an enumerable data structure that your class makes use of, with the familiar [] operator. Let&#8217;s say [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[105,57,106,104,103],"_links":{"self":[{"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/posts\/210"}],"collection":[{"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":3,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":213,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions\/213"}],"wp:attachment":[{"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}