{"id":292,"date":"2011-12-07T10:29:56","date_gmt":"2011-12-07T09:29:56","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=292"},"modified":"2011-12-07T10:29:56","modified_gmt":"2011-12-07T09:29:56","slug":"custom-objects-with-collection-initializers","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=292","title":{"rendered":"Custom Objects with Collection Initializers"},"content":{"rendered":"<p>Have you ever wondered whether it is possible to create your own collection initializers just like for the List&lt;T&gt;?<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nvar list = new List { 12, 42, 256, 1024 };\r\n<\/pre>\n<p>Well&#8230; doing that is actually extremely simple. All your custom object needs is a public Add method that takes whatever arguments you might want. Don&#8217;t try to analyze the code, I know that it doesn&#8217;t make any sense at all &#8211; it&#8217;s just an example<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npublic class MyCollection : IEnumerable\r\n{\r\n\tprivate List _list = new List();\r\n\r\n\tpublic void Add(Predicate predicate, T trueValue, T falseValue)\r\n\t{\r\n\t\t_list.Add(new MyIfThen { Predicate = predicate, TrueValue = trueValue, FalseValue = falseValue });\r\n\t}\r\n\r\n\tpublic struct MyIfThen\r\n\t{\r\n\t\tpublic Predicate Predicate;\r\n\t\tpublic T TrueValue;\r\n\t\tpublic T FalseValue;\r\n\t}\r\n\r\n\t#region IEnumerable Members\r\n\r\n\tpublic IEnumerator GetEnumerator()\r\n\t{\r\n\t\treturn _list.Select(ifthen =&gt; ifthen.TrueValue).GetEnumerator();\r\n\t}\r\n\r\n\t#endregion\r\n\r\n\t#region IEnumerable Members\r\n\r\n\tSystem.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()\r\n\t{\r\n\t\tGetEnumerator();\r\n\t}\r\n\r\n\t#endregion\r\n}\r\n\r\n...\r\n\r\nvar mine = new MyCollection\r\n\t\t   {\r\n\t\t\t   {i =&gt; i%2 == 0, 12, 42},\r\n\t\t\t   {i =&gt; i%3 == 0, 33, 66},\r\n\t\t   };\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wondered whether it is possible to create your own collection initializers just like for the List&lt;T&gt;? var list = new List { 12, 42, 256, 1024 }; Well&#8230; doing that is actually extremely simple. All your custom object needs is a public Add method that takes whatever arguments you might want. Don&#8217;t &hellip; <\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-executor","4":"post-292","6":"format-standard","7":"category-uncategorized"},"acf":[],"views":1978,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/292"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=292"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/292\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}