{"id":2649,"date":"2008-07-17T12:17:00","date_gmt":"2008-07-17T12:17:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/a-quick-net-puzzle\/"},"modified":"2016-07-28T10:49:20","modified_gmt":"2016-07-28T10:49:20","slug":"a-quick-net-puzzle","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/a-quick-net-puzzle\/","title":{"rendered":"A Quick .NET Puzzle"},"content":{"rendered":"<p><code><\/code><code>Just&#160;a quick&#160;.NET puzzle.&#160; Does this application ever throw that ApplicationException?&#160; If so, why?<\/code><\/p>\n<p><code><\/code><code><br \/>using System;<br \/>using System.Threading;<\/p>\n<p>class Program<br \/>{<br \/>&#160;&#160;&#160;&#160;static long Num = 0;<\/p>\n<p>&#160;&#160;&#160;&#160;static void Main(string[] args)<br \/>&#160;&#160;&#160;&#160;{<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Thread t1 = new Thread(ModifyNum);<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;t1.Start();<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;while (true)<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;long k = Num;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if (k != -1&#160;&amp;&amp; k != 0) throw new ApplicationException(<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\"k is not -1 or 0.&#160;&#160;It is \" + k.ToString());<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br \/>&#160;&#160;&#160;&#160;}<\/p>\n<p>&#160;&#160;&#160;&#160;static void ModifyNum()<br \/>&#160;&#160;&#160;&#160;{<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;while (true)<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if (Num == -1) Num = 0;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;else Num = -1;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br \/>&#160;&#160;&#160;&#160;}<br \/>}<br \/><\/code><\/p>\n<p><code>Stuck?&#160; Then here's a hint: if you change&#160;Num from a long to an int, it never throws the exception.<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just&#160;a quick&#160;.NET puzzle.&#160; Does this application ever throw that ApplicationException?&#160; If so, why? using System;using System.Threading; class Program{&#160;&#160;&#160;&#160;static long Num = 0; &#160;&#160;&#160;&#160;static void Main(string[] args)&#160;&#160;&#160;&#160;{&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Thread t1 = new Thread(ModifyNum);&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;t1.Start();&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;while (true)&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;long k = Num;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if (k != -1&#160;&amp;&amp; k != 0) throw new ApplicationException(&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#8221;k is not -1 or 0.&#160;&#160;It is &#8221; + k.ToString());&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}&#160;&#160;&#160;&#160;} &#160;&#160;&#160;&#160;static void ModifyNum()&#160;&#160;&#160;&#160;{&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;while&#8230;&hellip;<\/p>\n","protected":false},"author":95472,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2649","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/95472"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2649"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2649\/revisions"}],"predecessor-version":[{"id":41642,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2649\/revisions\/41642"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2649"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}