{"id":864,"date":"2012-11-14T17:25:51","date_gmt":"2012-11-14T16:25:51","guid":{"rendered":"https:\/\/www.kolja-engelmann.de\/blog\/?p=864"},"modified":"2012-11-14T17:25:51","modified_gmt":"2012-11-14T16:25:51","slug":"c-cannot-unbox-as-a-type","status":"publish","type":"post","link":"https:\/\/www.kolja-engelmann.de\/blog\/2012\/11\/c-cannot-unbox-as-a-type\/","title":{"rendered":"C# &#8211; Cannot unbox <variable> as a &#8218;type&#8216;"},"content":{"rendered":"<p>Dieses St\u00fcck Code fand sich in abgewandelter Form in einem meiner Programme<\/p>\n<pre class=\"lang:c# decode:true\">object _i = 4;\r\ndecimal _d = (decimal)_i; \/\/ Cannot unbox '_i' as a 'decimal' - InvalidCastException<\/pre>\n<p>Kann man pl\u00f6tzlich kein <code>int<\/code> mehr auf <code>double<\/code> casten? \u00a0H\u00e4tte ich mal aufgepasst, dann w\u00fcsste ich, dass ein unboxing (<code>(decimal)_i<\/code>) nur dann funktioniert, wenn der Zieldatentyp auch genau dem Ausgangsdatentyp entspricht. Im obigen Fall m\u00fcsste ich solch ein Konstrukt anwenden:<\/p>\n<pre class=\"lang:default mark:2 decode:true\">object _i = 4;\r\ndecimal _d = (decimal)(int)_i;<\/pre>\n<p>Ein d\u00e4mlicher Fehler, den man auch nur einmal macht, sobald man wei\u00df was man falsch gemacht hat.<\/p>\n<p>Done;<\/p>","protected":false},"excerpt":{"rendered":"<p>Dieses St\u00fcck Code fand sich in abgewandelter Form in einem meiner Programme object _i = 4; decimal _d = (decimal)_i; \/\/ Cannot unbox &#8218;_i&#8216; as a &#8218;decimal&#8216; &#8211; InvalidCastException Kann man pl\u00f6tzlich kein int mehr&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":867,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[38],"tags":[],"class_list":["post-864","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codeschnippsel"],"jetpack_featured_media_url":"https:\/\/www.kolja-engelmann.de\/blog\/wp-content\/uploads\/2012\/11\/c_sourcecode.jpg","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/comments?post=864"}],"version-history":[{"count":0,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/posts\/864\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/media\/867"}],"wp:attachment":[{"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/media?parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/categories?post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/tags?post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}