{"id":3658,"date":"2014-03-29T07:39:33","date_gmt":"2014-03-29T07:39:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/c-constants-and-delegates-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:42:27","modified_gmt":"2022-08-30T15:42:27","slug":"c-constants-and-delegates-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/c-constants-and-delegates-collection-of-common-programming-errors\/","title":{"rendered":"C# Constants and Delegates-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/051d009d2100bb58755461712bd283ed?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nxxmrlnxx<\/p>\n<p>I&#8217;ve seen delgates and constants before in code but when and where are the appropiate times to use these? The uses that I&#8217;ve seen I can see other ways to program around them? Could any one tell me there true benefits for I&#8217;ve never used either.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/f6bd7a872528410a6015669d349f097b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nFoxfire<\/p>\n<p>Delegates are an absolute must-have if you add events to your class or are doing anything asynchroneous (There are several other good reasons to have delegates). Benefits is that it is a very flexible approach.<\/p>\n<p>Constants help you to prevent &#8220;Magic Numbers&#8221;. Aka provide a central place where you can specify constant data that is semantically identical. Their benefit is that they incur absolutely no performance or memory overhead.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/80e497ec2173c1a6a4f28f0aec5a77a8?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAbel<\/p>\n<p>I&#8217;d like to emphasize the difference between <code>const<\/code> and <code>readonly<\/code> in C#, even though you don&#8217;t ask, it can be of importance:<\/p>\n<ol>\n<li>A <code>const<\/code> variable is replaced by its literal value when you compile. That means, if you change the value of it (i.e., add more digits to <code>PI<\/code>, or increase allowed <code>MAX_PROCESSORS<\/code>), and other components use this constant, they will not see the new value.<\/li>\n<li>A <code>readonly<\/code> variable cannot be changed either, but is never replaced by its literal value when you compile. When you update your reference, other components of your application will see this update immediately and do not need to be recompiled.<\/li>\n<\/ol>\n<p>This difference is subtle but very important as it can introduce subtle bugs. The lesson here is: <strong><em>only use <code>const<\/code> when you are absolutely sure the value will never change, use <code>readonly<\/code> otherwise<\/em><\/strong>.<\/p>\n<p>Delegates are a placeholder (blueprint, signature) of a method call. I consider them <em>the interface declaration of a method<\/em>. A delegate variable is of the type of a delegate. It can be used as if it were the method (yet it can point to different implementations of the same method signature).<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/pHxhf.jpg?s=32&amp;g=1\" \/><br \/>\nChrisF<\/p>\n<p>Constants should be used for values like pi (3.14159&#8230;). Using them means that your code reads sensibly:<\/p>\n<pre><code>double circumference = radius * 2.0 * PI;\n<\/code><\/pre>\n<p>it also means that if the value of the constant changes (obviously not for pi!) then you only have to change the code in one place.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/351abc94ca64fe4cfed5a4f7e99a8518?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRajesh Rolen- DotNet Developer<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>xxmrlnxx I&#8217;ve seen delgates and constants before in code but when and where are the appropiate times to use these? The uses that I&#8217;ve seen I can see other ways to program around them? Could any one tell me there true benefits for I&#8217;ve never used either. Foxfire Delegates are an absolute must-have if you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-3658","post","type-post","status-publish","format-standard","hentry","category-semantic","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3658","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=3658"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3658\/revisions"}],"predecessor-version":[{"id":8796,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3658\/revisions\/8796"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}