{"id":121,"date":"2009-02-06T11:21:54","date_gmt":"2009-02-06T16:21:54","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=121"},"modified":"2009-05-29T08:31:14","modified_gmt":"2009-05-29T13:31:14","slug":"understanding-read-only-properties-with-object-refererence-types","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2009\/02\/06\/understanding-read-only-properties-with-object-refererence-types\/","title":{"rendered":"Understanding Read-Only Properties with Object (Refererence Types)"},"content":{"rendered":"<pre>using System;\r\nusing System.Text;\r\n\r\nnamespace TestReadOnly\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            MyClass1 myObject1 = new MyClass1();\r\n\r\n            \/\/ myObject1.MyObject2 = new MyClass2();    \/\/ clearly illegal\r\n\r\n            \/\/ legal, reference is read-only, not object\r\n            myObject1.MyObject2.x = 1;\r\n            Console.WriteLine(\"myObject1.MyObject2.x: {0}\", myObject1.MyObject2.x);\r\n\r\n            \/\/ also legal, similar to above, anotherObject2 is just a reference to the same object\r\n            MyClass2 anotherObject2 = myObject1.MyObject2;\r\n            anotherObject2.x = 2;     \/\/probably will be allowed\r\n            Console.WriteLine(\"anotherObject2.x: {0}\", anotherObject2.x);\r\n\r\n            \/\/ also legal, anotherObject2 is now a reference to a completely new objcet\r\n            anotherObject2 = new MyClass2();\r\n            anotherObject2.x = 3;      \/\/ no affect on myObject1.Myobj2\r\n            Console.WriteLine(\"anotherObject2.x: {0}\", anotherObject2.x);\r\n\r\n            \/\/ show that myObject1.MyObject2 was not affected by last attempt\r\n            Console.WriteLine(\"myObject1.MyObject2.x: {0}\", myObject1.MyObject2.x);\r\n\r\n            Console.Read();\r\n        }\r\n    }\r\n\r\n    class MyClass1\r\n    {\r\n        private MyClass2 _object2 = new MyClass2();\r\n        public MyClass2 MyObject2\r\n        {\r\n            get{ return _object2; }\r\n        }\r\n    }\r\n\r\n    class MyClass2\r\n    {\r\n        public int x = 0;\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using System; using System.Text; namespace TestReadOnly { class Program { static void Main(string[] args) { MyClass1 myObject1 = new MyClass1(); \/\/ myObject1.MyObject2 = new MyClass2(); \/\/ clearly illegal \/\/ legal, reference is read-only, not object myObject1.MyObject2.x = 1; Console.WriteLine(&#8220;myObject1.MyObject2.x: {0}&#8221;, myObject1.MyObject2.x); \/\/ also legal, similar to above, anotherObject2 is just a reference to the same [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,34,18],"tags":[69],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-aspnet","category-microsoft","category-programming","tag-linkedin"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}