{"id":321,"date":"2016-08-03T19:41:46","date_gmt":"2016-08-03T17:41:46","guid":{"rendered":"http:\/\/tech.bpeeters.nl\/?p=321"},"modified":"2016-08-04T08:02:33","modified_gmt":"2016-08-04T06:02:33","slug":"prevent-changing-the-value-of-an-object-passed-through-a-method-in-c","status":"publish","type":"post","link":"https:\/\/brunopeeters.com\/blog\/prevent-changing-the-value-of-an-object-passed-through-a-method-in-c\/","title":{"rendered":"Prevent changing the value of an object passed through a method in C#"},"content":{"rendered":"<p class=\"aLF-aPX-K0-aPE aLF-aPX-aLK-ayr-auR\">Within C++, you have the\u00a0<em>const<\/em> keyword, that can be used to say to the\u00a0one which calls the method that the argument which\u00a0passed through the const parameter won&#8217;t be changed within the method.\u00a0This is\u00a0merely adding a contract. Sure, the compiler does some checking for you, but still it is possible to change the value of your const parameter. See for example this nice example (obtained from\u00a0<a href=\"https:\/\/isocpp.org\/wiki\/faq\/const-correctness#aliasing-and-const\">https:\/\/isocpp.org\/wiki\/faq\/const-correctness#aliasing-and-const<\/a>):<\/p>\n<p class=\"aLF-aPX-K0-aPE aLF-aPX-aLK-ayr-auR\"><!--more--><\/p>\n<pre class=\"aLF-aPX-K0-aPE aLF-aPX-aLK-ayr-auR\">void f(const int* p1, int* p2)\r\n{\r\n  int i = *p1;         \/\/ Get the (original) value of *p1\r\n  *p2 = 7;             \/\/ If p1 == p2, this will also change *p1\r\n  int j = *p1;         \/\/ Get the (possibly new) value of *p1\r\n  if (i != j) {\r\n    std::cout &lt;&lt; \"*p1 changed, but it didn't change via pointer p1!\\n\";\r\n    assert(p1 == p2);  \/\/ This is the only way *p1 could be different\r\n  }\r\n}\r\nint main()\r\n{\r\n  int x = 5;\r\n  f(&amp;x, &amp;x);           \/\/ This is perfectly legal (and even moral!)\r\n  \/\/ ...\r\n}\r\n<\/pre>\n<p class=\"aLF-aPX-K0-aPE aLF-aPX-aLK-ayr-auR\">In the example above, you can simply change variable x in the main method by parsing it twice to the method f. C# doesn&#8217;t have a const keyword for parameters. How could you ensure that your method doesn&#8217;t change anything to the parameter? I thought of some solutions that may do the trick:<\/p>\n<h2 class=\"aLF-aPX-K0-aPE aLF-aPX-aLK-ayr-auR\">Solution 1<\/h2>\n<p class=\"aLF-aPX-K0-aPE aLF-aPX-aLK-ayr-auR\">Just do what your method says it does. If the method changes the parameters, this should also represent in the name of the method. The method should call like <em>GetAddressAndChangePerson<\/em> if the method retrieves the address of a person and changes it.\u00a0If it changes stuff it doesn&#8217;t need to change, the changes are considered to be side-effects in your code.<\/p>\n<h2 class=\"aLF-aPX-K0-aPE aLF-aPX-aLK-ayr-auR\">Solution\u00a02<\/h2>\n<p class=\"aLF-aPX-K0-aPE aLF-aPX-aLK-ayr-auR\">Construct the method, provided in the method, in such a way it doesn&#8217;t allow changes to the object in the first place. Make setters of properties private. However, this is not always possible, since at other points you want to change the object or you don&#8217;t own the interface of the object.<\/p>\n<h2 class=\"aLF-aPX-K0-aPE aLF-aPX-aLK-ayr-auR\">Solution 3<\/h2>\n<p>The less neat solution is to make a deep copy of the object you passing through the method if you suspect a method changes the state of your object. However, this is not clean and violates\u00a0<em>solution 1<\/em> in the first place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Within C++, you have the\u00a0const keyword, that can be used to say to the\u00a0one which calls the method that the argument which\u00a0passed through the const parameter won&#8217;t be changed within the method.\u00a0This is\u00a0merely adding a contract. Sure, the compiler does some checking for you, but still it is possible to change the value of your [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5,3,11],"tags":[22,9],"class_list":["post-321","post","type-post","status-publish","format-standard","hentry","category-best-practices","category-csharp","category-cplusplus","tag-csharp","tag-c-2"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2VFWh-5b","_links":{"self":[{"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/posts\/321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/comments?post=321"}],"version-history":[{"count":4,"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/posts\/321\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/posts\/321\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/media?parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/categories?post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brunopeeters.com\/blog\/wp-json\/wp\/v2\/tags?post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}