{"id":90,"date":"2009-03-05T12:39:06","date_gmt":"2009-03-05T10:39:06","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=90"},"modified":"2009-05-16T09:55:55","modified_gmt":"2009-05-16T07:55:55","slug":"anonymous-types-and-generics","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=90","title":{"rendered":"Anonymous Types and Generics"},"content":{"rendered":"<p>C# 3.0 has a &#8220;nice&#8221; Feature called &#8220;Anonymous Types&#8221;.<\/p>\n<p>Basically it is great for LINQ where you want to create a Type on the fly.<br \/>\nTo create such a Type just use<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; toolbar: false; notranslate\" title=\"\">\r\nvar myAnonymousObject = new {\r\n    Firstname = &quot;John&quot;,\r\n    Lastname = &quot;Doe&quot;\r\n};\r\n<\/pre>\n<p>Now you possibly want to extend that a little and have a Function return a List of such a Type.<br \/>\nYou can&#8217;t use List&lt;T&gt; because you don&#8217;t know the Type. You could use a List&lt;object&gt; but that&#8217;s not nice. It&#8217;s better to use a Feature called &#8220;Casting by Example&#8221;. <\/p>\n<p>An Example Function implementing this Feature could look like:<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; toolbar: false; notranslate\" title=\"\">\r\npublic List&lt;T&gt; GetList&lt;T&gt;(T exampleObject)\r\n{\r\n    List&lt;T&gt; newList = new List&lt;T&gt;();\r\n    \/\/ TODO: Fill the List somehow\r\n    return newList;\r\n}\r\n<\/pre>\n<p>You can then call this Function like this:<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; toolbar: false; notranslate\" title=\"\">\r\nvar someList = GetList(\r\n    new {\r\n        Firstname = &quot;&quot;,\r\n        Lastname = &quot;&quot;\r\n    }\r\n);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C# 3.0 has a &#8220;nice&#8221; Feature called &#8220;Anonymous Types&#8221;. Basically it is great for LINQ where you want to create a Type on the fly. To create such a Type just use var myAnonymousObject = new { Firstname = &quot;John&quot;, Lastname = &quot;Doe&quot; }; Now you possibly want to extend that a little and have &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,3],"tags":[28,31,32,30,29],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-roemer","4":"post-90","6":"format-standard","7":"category-csharp","8":"category-programming","9":"post_tag-anonymous","10":"post_tag-casting","11":"post_tag-example","12":"post_tag-generic","13":"post_tag-type"},"acf":[],"views":1915,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/90"}],"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\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}