{"id":3824,"date":"2014-03-30T05:53:48","date_gmt":"2014-03-30T05:53:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-narrowing-conversion-wont-generate-exception-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:53:48","modified_gmt":"2014-03-30T05:53:48","slug":"c-narrowing-conversion-wont-generate-exception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-narrowing-conversion-wont-generate-exception-collection-of-common-programming-errors\/","title":{"rendered":"c# narrowing conversion won&#39;t generate exception-Collection of common programming errors"},"content":{"rendered":"<p>From the C# Type Conversion Tables(http:\/\/msdn.microsoft.com\/en-us\/library\/08h86h00.aspx): &#8220;A narrowing conversion can also result in a loss of information for other data types. However, an OverflowException is thrown if the value of a type that is being converted falls outside of the range specified by the target type&#8217;s MaxValue and MinValue fields, and the conversion is checked by the runtime to ensure that the value of the target type does not exceed its MaxValue or MinValue.&#8221;<\/p>\n<p>So I was expecting the following code to generate an exception:<\/p>\n<pre><code>static void Main() {\n    int numb1 = 333333333;\n    short numb2 = (short)numb1;\n\n    Console.WriteLine(\"Value of numb1 is {0}\", numb1);\n    Console.WriteLine(\"Type of numb1 is {0}\", numb1.GetType());\n    Console.WriteLine(\"MinValue of int is {0}\", int.MinValue);\n    Console.WriteLine(\"MaxValue of int is {0}\\n\", int.MaxValue);\n\n    Console.WriteLine(\"Value of numb2 is {0}\", numb2);\n    Console.WriteLine(\"Type of numb2 is {0}\", numb2.GetType());\n    Console.WriteLine(\"MinValue of short is {0}\", short.MinValue);\n    Console.WriteLine(\"MaxValue of short is {0}\", short.MaxValue);\n\n    Console.ReadKey();\n}\n<\/code><\/pre>\n<p>but instead numb2 gets a value of 17237. I don&#8217;t know where this value comes from and I really don&#8217;t understand why an overflow exception wasn&#8217;t generated.<\/p>\n<p>Any suggestion is highly appreciated! Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From the C# Type Conversion Tables(http:\/\/msdn.microsoft.com\/en-us\/library\/08h86h00.aspx): &#8220;A narrowing conversion can also result in a loss of information for other data types. However, an OverflowException is thrown if the value of a type that is being converted falls outside of the range specified by the target type&#8217;s MaxValue and MinValue fields, and the conversion is checked [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3824","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3824"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3824\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}