{"id":75017,"date":"2022-09-14T08:00:52","date_gmt":"2022-09-14T06:00:52","guid":{"rendered":"https:\/\/drafts.code-maze.com\/?p=75017"},"modified":"2022-09-14T08:55:49","modified_gmt":"2022-09-14T06:55:49","slug":"csharp-indexers","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-indexers\/","title":{"rendered":"Indexers in C#"},"content":{"rendered":"<p>In this article, we are going to learn about Indexers in C#, how to declare them in a class, struct, or interface, and overload them. We&#8217;re also going to learn what the differences between indexers and properties are.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/csharp-basic-topics\/IndexersInCSharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s start.<\/p>\n<h2><a id=\"whatareindexers\"><\/a>What Are Indexers and What Do Indexers in C# Do?<\/h2>\n<p>We&#8217;re all used to dealing with <a href=\"https:\/\/code-maze.com\/csharp-basics-arrays\/\" target=\"_blank\" rel=\"noopener\">arrays<\/a>. The array is easy to use and most importantly its elements are indexed, i.e. we can access any element by its <strong>index<\/strong>. But what if we want to add indexing to our class or struct?<\/p>\n<p>This is precisely what the Indexers achieve for us.<\/p>\n<p><strong>Indexer allows instances of a class or struct to have indexes.<\/strong> It enables us to do things that arrays alone cannot, and we&#8217;re going to see what exactly these things are going forward.<\/p>\n<h2><a id=\"indexersyntax\"><\/a>How to Implement Indexers in C#<\/h2>\n<p>Let&#8217;s start by checking out the Indexers syntax. We can define Indexer by using the <code>this<\/code> keyword:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[acccess modifier] [return type] this([parameters])\r\n{\r\n    get \r\n    { \r\n        \/\/return a value \r\n    }\r\n    set \r\n    { \r\n        \/\/set a value\r\n    }\r\n}<\/pre>\n<p><strong>access modifier: <\/strong>Can be public, private, protected, or internal.<\/p>\n<p><strong>return type:<\/strong> Can be any type we need, it certainly can&#8217;t be <strong>void<\/strong>.<\/p>\n<p><strong>parameters:<\/strong> Can be any type and not just int type as in arrays. We should define one parameter at least.<\/p>\n<p><strong>get accessor<\/strong>: Returns a value.<\/p>\n<p><strong>set accessor:<\/strong> Assigns a value.<\/p>\n<p>We should define either <code>set<\/code> or <code>get<\/code> accessor at least.<\/p>\n<h3 id=\"expression-body-definitions\" class=\"heading-anchor\"><a id=\"expressionbodydefinitions\"><\/a>Expression Body Definitions<\/h3>\n<p>When the body of the <code>get<\/code> accessor is a single statement, we can use lambda (<code>=&gt;<\/code>) <a href=\"https:\/\/code-maze.com\/lambda-expressions-in-csharp\/\" target=\"_blank\" rel=\"noopener\">expression<\/a>.<\/p>\n<p>Beginning with C# 7.0, we can also use this expression with the <code>set<\/code> accessor:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[acccess\u00a0modifier]\u00a0[return\u00a0type]\u00a0this([parameters])\r\n{\r\n    get =&gt; \/\/ single statment\r\n    set =&gt; \/\/ single statment\r\n}\r\n<\/pre>\n<h2><a id=\"singledimensionalmap\"><\/a>Create One-Dimensional Arrays With Indexers<\/h2>\n<p>When the indexer has only one parameter, this is similar to one-dimensional arrays:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Sentence\r\n{\r\n    private string[] _words;\r\n    public\u00a0int\u00a0Length\u00a0=&gt;\u00a0_words.Length;\u00a0\r\n    \r\n    public Sentence(string sentence)\r\n    {\r\n        _words = sentence.Split(' ');\r\n    }\r\n\r\n    public string this[int wordNumber]\r\n    {\r\n        get\r\n        {\r\n            return _words[wordNumber];\r\n        }\r\n        set\r\n        {\r\n            _words[wordNumber] = value;\r\n        }\r\n    }\r\n}<\/pre>\n<p>We create a\u00a0<code>Sentence<\/code> class, it contains a <code>_words<\/code> array and the Indexer takes an <strong>int <\/strong>type as a parameter, its modifier is <strong>public<\/strong> and the return type is <strong>string<\/strong>.<\/p>\n<p>In the constructor, we split the sentence into words to fill the <code>_words<\/code> array.<\/p>\n<p>Now, we can use the <code>Sentence<\/code> class as an array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var sentence = new Sentence(\"Hello from Code Maze\");\r\nfor (int i = 0; i &lt; sentence.Length; i++)\r\n{\r\n    Console.WriteLine(\"Word {0} : {1}\", i + 1, sentence[i]);\r\n}\r\n\r\n\/\/ The output is:\r\n\/\/ Word 1 : Hello\r\n\/\/ Word 2 : from \r\n\/\/ Word 3 : Code \r\n\/\/ Word 4 : Maze<\/pre>\n<p>So far this looks pretty similar to what we can do with arrays, so what is the difference between indexers and arrays exactly here?<\/p>\n<p>One of the advantages of Indexers is that we can add validation to the input:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"8,9,18,19,25,26,28,29\">public class Sentence\r\n{\r\n    private string[] _words;\r\n    public int Length =&gt; _words.Length;\r\n\r\n    public Sentence(string sentence)\r\n    {\r\n        if (sentence == null)\r\n            throw new ArgumentNullException(nameof(sentence));\r\n\r\n        _words = sentence.Split(' ');\r\n    }\r\n\r\n    public string this[int wordNumber]\r\n    {\r\n        get\r\n        {\r\n            if (wordNumber &lt; 0 || wordNumber &gt; _words.Length - 1)\r\n                throw new ArgumentOutOfRangeException(nameof(wordNumber));\r\n\r\n            return _words[wordNumber];\r\n        }\r\n        set\r\n        {\r\n            if (wordNumber &lt; 0 || wordNumber &gt; _words.Length - 1)\r\n                throw new ArgumentOutOfRangeException(nameof(wordNumber));\r\n\r\n            if (value == null || value.Trim().Length == 0 || value.Split(' ').Length &gt; 1)\r\n                return;\r\n\r\n            _words[wordNumber] = value;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>In the constructor, we add a condition to check if <code>sentence<\/code> is null.<\/p>\n<p>In the <code>get<\/code> accessor we\u00a0make sure that the <code>wordNumber<\/code> is within the allowed range and in the <code>set<\/code> accessor\u00a0we do this too and in addition, we make sure the <code>value<\/code> is valid.<\/p>\n<h2><a id=\"multi-dimensionalmap\"><\/a>Create Multi-Dimensional Arrays With Indexers<\/h2>\n<p>As we&#8217;ve mentioned, we can pass more than one parameter to Indexers, similarly to how we do it with multi-dimensional arrays.<\/p>\n<p>A good example of this is the <strong>Tic-Tac-Toe<\/strong> game.<\/p>\n<p>First, let&#8217;s create an enum that contains our <code>CellStatus<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public enum CellStatus\r\n{\r\n    Empty,\r\n    X,\r\n    O\r\n}<\/pre>\n<p>Then, let&#8217;s create <code>TicTacToe<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class TicTacToe\r\n{\r\n    private const int _rowCount = 3;\r\n    private const int _colCount = 3;\r\n    private CellStatus[,] _patch = new CellStatus[_rowCount, _colCount];\r\n    public CellStatus this[int row, int col]\r\n    {\r\n        get\r\n        {\r\n            if (row &gt;= _rowCount || row &lt; 0)\r\n                throw new ArgumentOutOfRangeException(nameof(row));\r\n            if (col &gt;= _colCount || col &lt; 0)\r\n                throw new ArgumentOutOfRangeException(nameof(col));\r\n\r\n            return _patch[row, col];\r\n        }\r\n        set\r\n        {\r\n            if (row &gt;= _rowCount || row &lt; 0)\r\n                throw new ArgumentOutOfRangeException(nameof(row));\r\n            if (col &gt;= _colCount || col &lt; 0)\r\n                throw new ArgumentOutOfRangeException(nameof(col));\r\n            if (!Enum.IsDefined(value))\r\n                return;\r\n            if (value == CellStatus.Empty)\r\n                return;\r\n            if (_patch[row, col] != CellStatus.Empty)\r\n                return;\r\n            _patch[row, col] = value;\r\n        }\r\n    }\r\n\r\n    public override string ToString()\r\n    {\r\n        string s = \"\";\r\n        for (int i = 0; i &lt; _rowCount; i++)\r\n        {\r\n            for (int j = 0; j &lt; _colCount; j++)\r\n            {\r\n                s += _patch[i, j].ToString();\r\n                s += \"\\t\";\r\n            }\r\n            s += Environment.NewLine;\r\n        }\r\n\r\n        return s;\r\n    }\r\n}<\/pre>\n<p>Similar to the previous example, here we have two parameters representing row number and column number, respectively.<\/p>\n<p>We can use the <code>TicTacToe<\/code> class as a two-dimensional array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var ticTacToe = new TicTacToe();\r\nticTacToe[0, 0] = CellStatus.X;\r\nConsole.WriteLine(ticTacToe.ToString());\r\n\r\n\/\/ The output is:\r\n\/\/ X Empty Empty\r\n\/\/ Empty Empty Empty\r\n\/\/ Empty Empty Empty<\/pre>\n<h2><a id=\"overloadedindexers\"><\/a>Overloaded Indexers<\/h2>\n<p>Indexers can also be overloaded, i.e we can define more than one indexer in our class or our struct.<\/p>\n<p>Let&#8217;s apply this to the previous example, we want access to the cell by one integer index representing a cell number.<\/p>\n<p>Cell numbering begins with 1, and ends with 9, where cell(0,0) becomes cell(1), cell(0,1) becomes cell(2), etc ..<\/p>\n<p>We will add a simple method to archive a conversion process:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private static (int, int) Convert(int cellNumber)\r\n{\r\n    if(cellNumber &lt;= 0)\r\n        throw new ArgumentOutOfRangeException(nameof(cellNumber));\r\n\r\n    return ((cellNumber - 1) \/ _rowCount, (cellNumber - 1) % _colCount);\r\n}<\/pre>\n<p>And we let&#8217;s add another Indexer with one parameter:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public CellStatus this[int cellNumber]\r\n{\r\n    get\r\n    {\r\n        var result = Convert(cellNumber);\r\n        return this[result.Item1, result.Item2];\r\n    }\r\n    set\r\n    {\r\n        var result = Convert(cellNumber);\r\n        this[result.Item1, result.Item2] = value;\r\n    }\r\n}\r\n<\/pre>\n<p>We just convert the cell number to a row and a column and then call the other indexer.\u00a0<\/p>\n<p>Now, we can use <code>TicTacToe<\/code> class with one index:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">ticTacToe[9] = CellStatus.O;\r\nConsole.WriteLine(ticTacToe.ToString());\r\n\r\n\/\/ The output is:\r\n\/\/ X Empty Empty\r\n\/\/ Empty Empty Empty\r\n\/\/ Empty Empty O<\/pre>\n<h2><a id=\"indexersininterfaces\"><\/a>Indexers in Interfaces<\/h2>\n<p>If we need to, we can also declare indexers in <a href=\"https:\/\/code-maze.com\/csharp-interfaces\/\" target=\"_blank\" rel=\"noopener\">interfaces<\/a> the same way we do in classes, except interface accessors don&#8217;t have modifiers:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public interface IIndexerInterface\r\n{ \r\n    string this[int index] { get; }\r\n}\r\n\r\npublic class IndexerClass : IIndexerInterface\r\n{\r\n    public string this[int index] \r\n    { \r\n        get =&gt; \"Hello from class.\"; \r\n    }\r\n}\r\n<\/pre>\n<p>We notice that a <code>get<\/code> interface accessor does not have a body but after a <strong>default interface methods <\/strong>feature\u00a0in C# 8.0, we can write a default body.<\/p>\n<p>Let&#8217;s add another indexer with a default implementation:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">string this [string name] \r\n{\r\n    get =&gt; \"Hello from interface.\";\r\n}<\/pre>\n<p>This indexer does not need to be implemented in <code>IndexerClass<\/code> class because the <code>get<\/code> accessor has a default body.<\/p>\n<h2><a id=\"comparisonbetweenpropertiesandindexers\"><\/a>Comparison Between Properties and Indexers in C#<\/h2>\n<p>Indexers resemble properties but there are some differences between them:<\/p>\n\n<table id=\"tablepress-43\" class=\"tablepress tablepress-id-43\">\n<thead>\n<tr class=\"row-1\">\n\t<th class=\"column-1\">Indexer<\/th><th class=\"column-2\">Property<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\">Defined by this keyword.<\/td><td class=\"column-2\">Defined by a name.<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">Accessed by an index.<\/td><td class=\"column-2\">Accessed by a name.<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\">Can be only an instance member.<\/td><td class=\"column-2\">Can be a static or an instance member.<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\">A get accessor has the same formal parameter list<br \/>\nas the indexer<\/td><td class=\"column-2\">A get accessor has no parameters.<\/td>\n<\/tr>\n<tr class=\"row-6\">\n\t<td class=\"column-1\">A set accessor has the same formal parameter list <br \/>\nas the indexer, and also to the value parameter.<\/td><td class=\"column-2\">A set accessor contains the implicit value parameter.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-43 from cache -->\n<h2>When We Should Use Indexers?<\/h2>\n<p><span style=\"font-weight: 400;\">We should use Indexers when our class, struct, or interface has an internal collection that needs to be encapsulated and accessed as an array. This way we can use our class\/struct as a data structure.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">B<\/span><span style=\"font-weight: 400;\">esides the convenience and simplified syntax, Indexers provide us with more access flexibility and more features than arrays do, like the validation for example.<\/span><\/p>\n<h2><a id=\"conclusion\"><\/a>Conclusion<\/h2>\n<p>In this article, we\u2019ve learned a lot about Indexers in C#. We have seen how to define single or multi-dimensional indexers and how overloaded them and we have compared them with the properties.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn about Indexers in C#, how to declare them in a class, struct, or interface, and overload them. We&#8217;re also going to learn what the differences between indexers and properties are. Let&#8217;s start. What Are Indexers and What Do Indexers in C# Do? We&#8217;re all used to dealing [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[505,12],"tags":[730,1426],"class_list":["post-75017","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basic","category-csharp","tag-basic","tag-indexers","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Indexers in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"Indexers in C# allow instances of a class or struct to have indexes, which in turn enable us to do things we cannot do with arrays.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-indexers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Indexers in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"Indexers in C# allow instances of a class or struct to have indexes, which in turn enable us to do things we cannot do with arrays.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-indexers\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-14T06:00:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-14T06:55:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Indexers in C#\",\"datePublished\":\"2022-09-14T06:00:52+00:00\",\"dateModified\":\"2022-09-14T06:55:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/\"},\"wordCount\":801,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"Basic\",\"Indexers\"],\"articleSection\":[\"Basic\",\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-indexers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/\",\"url\":\"https:\/\/code-maze.com\/csharp-indexers\/\",\"name\":\"Indexers in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2022-09-14T06:00:52+00:00\",\"dateModified\":\"2022-09-14T06:55:49+00:00\",\"description\":\"Indexers in C# allow instances of a class or struct to have indexes, which in turn enable us to do things we cannot do with arrays.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-indexers\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-indexers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Indexers in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Indexers in C# - Code Maze","description":"Indexers in C# allow instances of a class or struct to have indexes, which in turn enable us to do things we cannot do with arrays.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-indexers\/","og_locale":"en_US","og_type":"article","og_title":"Indexers in C# - Code Maze","og_description":"Indexers in C# allow instances of a class or struct to have indexes, which in turn enable us to do things we cannot do with arrays.","og_url":"https:\/\/code-maze.com\/csharp-indexers\/","og_site_name":"Code Maze","article_published_time":"2022-09-14T06:00:52+00:00","article_modified_time":"2022-09-14T06:55:49+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-indexers\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-indexers\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Indexers in C#","datePublished":"2022-09-14T06:00:52+00:00","dateModified":"2022-09-14T06:55:49+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-indexers\/"},"wordCount":801,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-indexers\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["Basic","Indexers"],"articleSection":["Basic","C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-indexers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-indexers\/","url":"https:\/\/code-maze.com\/csharp-indexers\/","name":"Indexers in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-indexers\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-indexers\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2022-09-14T06:00:52+00:00","dateModified":"2022-09-14T06:55:49+00:00","description":"Indexers in C# allow instances of a class or struct to have indexes, which in turn enable us to do things we cannot do with arrays.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-indexers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-indexers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-indexers\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-indexers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Indexers in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/75017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=75017"}],"version-history":[{"count":6,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/75017\/revisions"}],"predecessor-version":[{"id":75045,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/75017\/revisions\/75045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=75017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=75017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=75017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}