{"id":566,"date":"2004-02-16T21:40:47","date_gmt":"2004-02-17T01:40:47","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=566"},"modified":"2004-02-16T21:40:47","modified_gmt":"2004-02-17T01:40:47","slug":"c-public-properties-vs-java-getx-and-setx","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/02\/16\/c-public-properties-vs-java-getx-and-setx\/","title":{"rendered":"C# Public Properties vs. Java getX() and setX()"},"content":{"rendered":"<p><a href=\"http:\/\/www.martinfowler.com\/\">Martin Fowler<\/a> <a href=\"http:\/\/www.martinfowler.com\/bliki\/PublicCsharpFields.html\">blogged<\/a> about <a href=\"http:\/\/msdn.microsoft.com\/library\/?url=\/library\/en-us\/csref\/html\/vclrfUsingProperties.asp?frame=true\">C# properties<\/a> a couple weeks back saying that he &#8220;<i>.. liked the notion of properties right from the start..<\/i>&#8221; because it&#8217;s &#8220;<i>.. much more natural to write obj.X = other.X.<\/i>&#8221; Further, &#8221; <i>&#8230; from a language point of view, properties and fields look the same. So if I have a field x which I can just read and write to, I can just declare it as a field. I don&#8217;t worry about this violating encapsulation, because should I wish to do something more fancy I can just replace it with a property later. It all saves a lot of typing of stupid accessor functions.<\/i>&#8221; For those of you who have never used C#, instead of writing this in Java:<br \/>\n<code><br \/>\n\/\/ Java getter &amp; setter<br \/>\nprivate String foo;<br \/>\npublic String getFoo() {<br \/>\n&nbsp;&nbsp;return this.foo;<br \/>\n}<br \/>\npublic void setFoo(String foo) {<br \/>\n&nbsp;&nbsp;this.foo = foo;<br \/>\n}<br \/>\n\/\/ access the private variable foo like this:<br \/>\nbar = obj.getFoo();<br \/>\n<\/code><br \/>\nyou can write this:<br \/>\n<code><br \/>\n\/\/ C# public property<br \/>\nprivate String _foo;<br \/>\npublic String Foo {<br \/>\n&nbsp;&nbsp;get {&nbsp; return _foo; }<br \/>\n&nbsp;&nbsp;set {&nbsp; _foo = value; }<br \/>\n}<br \/>\n\/\/ access the private variable _foo like this:<br \/>\nbar = obj.Foo;<br \/>\n<\/code><br \/>\nI initially liked this feature of C# as well, it definitely seems more natural to use properties.  Also, like Martin mentions, it saves you from having to make decisions about encapsulation.. do you use a getX()\/setX() method or do I just mark it as public?  With C#, you can change the implementation without changing your published API.<\/p>\n<p>Martin points out that in this case beauty is only skin deep. If you use reflection to access the public properties or fields of a C# class, you might be surprised to see that the implementation does in fact change.   In the example above, the property &#8216;Foo&#8217; gets turned into 2 methods: get_Foo() and set_Foo(), which you can see using reflection:<br \/>\n<code><br \/>\nType type = (typeof(YourClassName));<br \/>\nMemberInfo [] members;<br \/>\nmembers = type.GetMembers(BindingFlags.Public|BindingFlags.Instance);<br \/>\nConsole.WriteLine( \"\\nThe public instance members of class '{0}' are : \\n\", type);<br \/>\nfor (int i =0 ; i<br \/>\nMartin ends by noting \"<i>... now I have to write stupid accessor functions for accessible data values.<\/i>\" which is something that your IDE can do for you. In Eclipse, declare your private instance variables and then right click --&gt; Source --&gt; Generate Getter &amp; Setter...<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Martin Fowler blogged about C# properties a couple weeks back saying that he &#8220;.. liked the notion of properties right from the start..&#8221; because it&#8217;s &#8220;.. much more natural to write obj.X = other.X.&#8221; Further, &#8221; &#8230; from a language point of view, properties and fields look the same. So if I have a field &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/02\/16\/c-public-properties-vs-java-getx-and-setx\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C# Public Properties vs. Java getX() and setX()<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/566"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=566"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/566\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}