{"id":6055,"date":"2022-09-01T04:28:55","date_gmt":"2022-08-31T22:58:55","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/"},"modified":"2022-09-01T04:28:55","modified_gmt":"2022-08-31T22:58:55","slug":"solved-implementation-of-different-indexers-within-c-class","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/","title":{"rendered":"[Solved] Implementation of different indexers within C# class"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-28984987\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"28984987\" data-parentid=\"28984833\" data-score=\"4\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Using explicit implementation of interfaces, you can do something like:<\/p>\n<pre><code>public interface IMyClass\n{\n    object this[int index]\n    {\n        get; set;\n    }\n}\n\npublic class MyClass : IMyClass\n{\n    public string this[int index]\n    {\n        get\n        {\n            return \"\";\n        }\n\n        set\n        {\n        }\n    }\n\n    object IMyClass.this[int index]\n    {\n        get\n        {\n            return \"\";\n        }\n\n        set\n        {\n        }\n    }\n}\n<\/code><\/pre>\n<p>so implement two different enumerators with the same signature (where signature  = type\/number of input parameters). Note that you can have multiple interfaces, each one with an indexer with a different return type, so given x interfaces and one class, you can have x + 1 distinct indexers with the same signature.<\/p>\n<p>Now&#8230; If <code>Student<\/code> was a &#8220;real&#8221; class instead of an <code>object<\/code>, you could do some tricks with implicit casts (you can&#8217;t do implicit casts with\/against the <code>object<\/code> class):<\/p>\n<pre><code>public class StudentSpecificCollection\n{\n    private string[] arrName;\n    private int[] arrAge;\n    private object[] arrStudent;\n\n    public MyObject this[int index]\n    {\n        get\n        {\n            return new MyObject(this, index);\n        }\n\n        set\n        {\n            if (value.Type.HasFlag(MyObject.MyObjectType.Name))\n            {\n                arrName[index] = value;\n            }\n\n            if (value.Type.HasFlag(MyObject.MyObjectType.Age))\n            {\n                arrAge[index] = value;\n            }\n\n            if (value.Type.HasFlag(MyObject.MyObjectType.Student))\n            {\n                arrStudent[index] = value;\n            }\n        }\n    }\n\n    public class MyObject\n    {\n        [Flags]\n        public enum MyObjectType\n        {\n            Name = 1,\n            Age = 2,\n            Student = 4\n        }\n\n        public readonly MyObjectType Type;\n        public readonly string Name;\n        public readonly int Age;\n        public readonly object Student;\n\n        protected MyObject(string name)\n        {\n            Type = MyObjectType.Name;\n            Name = name;\n        }\n\n        protected MyObject(int age)\n        {\n            Type = MyObjectType.Age;\n            Age = age;\n        }\n\n        protected MyObject(object student)\n        {\n            Type = MyObjectType.Student;\n            Student = student;\n        }\n\n        public MyObject(StudentSpecificCollection obj, int ix)\n        {\n            Name = obj.arrName[ix];\n            Age = obj.arrAge[ix];\n            Student = obj.arrStudent[ix];\n        }\n\n        public static implicit operator string(MyObject obj)\n        {\n            if (!obj.Type.HasFlag(MyObjectType.Name))\n            {\n                throw new Exception();\n            }\n\n            return obj.Name;\n        }\n\n        public static implicit operator int(MyObject obj)\n        {\n            if (!obj.Type.HasFlag(MyObjectType.Age))\n            {\n                throw new Exception();\n            }\n\n            return obj.Age;\n        }\n\n        \/\/public static implicit operator object(MyObject obj)\n        \/\/{\n        \/\/    if (!obj.Type.HasFlag(MyObjectType.Student))\n        \/\/    {\n        \/\/        throw new Exception();\n        \/\/    }\n\n        \/\/    return obj.Student;\n        \/\/}\n\n        public static implicit operator MyObject(string name)\n        {\n            return new MyObject(name);\n        }\n\n        public static implicit operator MyObject(int age)\n        {\n            return new MyObject(age);\n        }\n\n        \/\/public static implicit operator MyObject(object student)\n        \/\/{\n        \/\/    return new MyObject(student);\n        \/\/}\n    }\n}\n<\/code><\/pre>\n<p>(the indexer returns a <code>MyObject<\/code> class that then can be implicitly casted to <code>int<\/code>\/<code>string<\/code>)<\/p>\n<p>then use as<\/p>\n<pre><code>var obj = new StudentSpecificCollection();\n\/\/ Load some students here\nobj[0] = \"Foo\";\nobj[0] = 25;\nstring name = obj[0];\nint age = obj[0];\n<\/code><\/pre>\n<p>Note that I <strong>don&#8217;t<\/strong> think this is a good idea. But <strong>you<\/strong> asked for it.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">4<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Implementation of different indexers within C# class <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Using explicit implementation of interfaces, you can do something like: public interface IMyClass { object this[int index] { get; set; } } public class MyClass : IMyClass { public string this[int index] { get { return &#8220;&#8221;; } set { } } object IMyClass.this[int index] { get { return &#8220;&#8221;; } set { } &#8230; <a title=\"[Solved] Implementation of different indexers within C# class\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/\" aria-label=\"More on [Solved] Implementation of different indexers within C# class\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[352,324,1616,996,547],"class_list":["post-6055","post","type-post","status-publish","format-standard","hentry","category-solved","tag-net","tag-c","tag-collections","tag-interface","tag-oop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Implementation of different indexers within C# class - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Implementation of different indexers within C# class - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Using explicit implementation of interfaces, you can do something like: public interface IMyClass { object this[int index] { get; set; } } public class MyClass : IMyClass { public string this[int index] { get { return &quot;&quot;; } set { } } object IMyClass.this[int index] { get { return &quot;&quot;; } set { } ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-31T22:58:55+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Implementation of different indexers within C# class\",\"datePublished\":\"2022-08-31T22:58:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/\"},\"wordCount\":136,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\".net\",\"c++\",\"collections\",\"interface\",\"oop\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/\",\"name\":\"[Solved] Implementation of different indexers within C# class - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-31T22:58:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Implementation of different indexers within C# class\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Implementation of different indexers within C# class - JassWeb","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:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Implementation of different indexers within C# class - JassWeb","og_description":"[ad_1] Using explicit implementation of interfaces, you can do something like: public interface IMyClass { object this[int index] { get; set; } } public class MyClass : IMyClass { public string this[int index] { get { return \"\"; } set { } } object IMyClass.this[int index] { get { return \"\"; } set { } ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/","og_site_name":"JassWeb","article_published_time":"2022-08-31T22:58:55+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Implementation of different indexers within C# class","datePublished":"2022-08-31T22:58:55+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/"},"wordCount":136,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":[".net","c++","collections","interface","oop"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/","url":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/","name":"[Solved] Implementation of different indexers within C# class - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-31T22:58:55+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-implementation-of-different-indexers-within-c-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Implementation of different indexers within C# class"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/6055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=6055"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/6055\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=6055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=6055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=6055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}