{"id":1573,"date":"2009-09-04T17:01:47","date_gmt":"2009-09-04T17:01:47","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2009\/09\/04\/properties-vs-fields-why-does-it-matter-jonathan-aneja\/"},"modified":"2024-07-05T12:46:53","modified_gmt":"2024-07-05T19:46:53","slug":"properties-vs-fields-why-does-it-matter-jonathan-aneja","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/properties-vs-fields-why-does-it-matter-jonathan-aneja\/","title":{"rendered":"Properties vs Fields &#8211; Why Does it Matter? (Jonathan Aneja)"},"content":{"rendered":"<\/p>\n<p><font size=\"3\"><\/font><\/p>\n<p><font size=\"3\" face=\"Calibri\">The other day I got an email from a developer asking an interesting question about VB10\u2019s <\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd293589(VS.100).aspx\"><font size=\"3\" face=\"Calibri\">Auto-implemented Properties<\/font><\/a><font size=\"3\" face=\"Calibri\"> feature.<\/font><\/p>\n<p><font size=\"3\"><font face=\"Calibri\"><em>\u201cI am, however, somewhat confused regarding the properties feature you mentioned &#8211; i.e. turning 11 lines of get\/set\/backing variable into one line. Wouldn&#8217;t this be exactly the same as the existing functionality of creating a public variable? I see no benefit for using a property in this case, except perhaps for the sake of what some would consider best practice.\u201d<\/em> <\/font><\/font><\/p>\n<p> <i><\/i>  <\/p>\n<p><font size=\"3\" face=\"Calibri\">On the surface there doesn\u2019t seem to be a big difference between this:<\/font><\/p>\n<p><font size=\"3\" face=\"Consola\"><font color=\"#0000ff\">Public Property<\/font> Name() <font color=\"#0000ff\">As String<\/font><\/font><\/p>\n<p><font size=\"3\" face=\"Calibri\">And this:<\/font><\/p>\n<p><font size=\"3\" face=\"Consola\"><font color=\"#0000ff\">Public <\/font>Name() <font color=\"#0000ff\">As String<\/font><\/font><\/p>\n<p><font size=\"3\"><font face=\"Calibri\">Clearly both will expose an object\u2019s state to the outside world, and can be read\/modified using the exact same syntax. Which then begs the question<i>, why would I ever use a property if I have no special logic in the Getter\/Setter?<\/i><\/font><\/font><\/p>\n<p><font size=\"3\" face=\"Calibri\">Let\u2019s consider some reasons: <\/font><\/p>\n<p><strong><font size=\"4\" face=\"Calibri\">1. Fields can\u2019t be used in Interfaces<\/font><\/strong><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/7\/2019\/02\/clip_image002_2.jpg\"><font size=\"3\" face=\"Calibri\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"clip_image002\" border=\"0\" alt=\"clip_image002\" src=\"https:\/\/devblogs.microsoft.com\/vbteam\/wp-content\/uploads\/sites\/7\/2009\/09\/clip_image002_thumb.jpg\" width=\"473\" height=\"83\" \/><\/font><\/a><\/p>\n<p><font size=\"3\" face=\"Calibri\">You can\u2019t enforce the existence of a field in an object\u2019s public contract through an interface. For properties though it works fine.<\/font><\/p>\n<p><font size=\"4\"><font face=\"Calibri\"><b>2. <\/b><b>Validation<\/b><\/font><\/font><\/p>\n<p><font size=\"3\" face=\"Calibri\">While your application currently may not require any validation logic to set a particular value, changing business requirements may require inserting this logic later. At that point changing a field to a property is a breaking change for consumers of your API. (For example if someone was inspecting your class via reflection).<\/font><\/p>\n<p><font size=\"4\"><font face=\"Calibri\"><b>3. <\/b><b>Binary Serialization<\/b><\/font><\/font><\/p>\n<p><font size=\"3\" face=\"Calibri\">Changing a field to a property is a breaking change if you\u2019re using binary serialization. Incidentally, this is one of the reasons VB10\u2019s auto-implemented properties have a \u201cbindable\u201d backing field (i.e. you can express the name of the backing field in code) \u2013 that way, if you change an auto-implemented property to an expanded property, you can still maintain serialization compatibility by keeping the backing field name the same (in C# you\u2019re forced to change it because it generates backing fields with unbindable names).<\/font><\/p>\n<p><font size=\"4\"><font face=\"Calibri\"><b>4. <\/b><b>A lot of the .NET databinding infrastructure binds to properties but not fields<\/b><\/font><\/font><\/p>\n<p><font size=\"3\" face=\"Calibri\">I\u2019ve heard arguments on both sides as to whether or not that\u2019s a good thing, but the reality is that\u2019s the way it works right now.<\/font><\/p>\n<p> <b><\/b>  <\/p>\n<p><font size=\"4\"><font face=\"Calibri\"><b>5. <\/b><b>Exposing a public field is an <\/b><\/font><\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms182141(VS.80).aspx\"><b><font size=\"4\" face=\"Calibri\">FxCop violation<\/font><\/b><\/a><b><\/b><\/p>\n<p><font size=\"3\" face=\"Calibri\">For many of the reasons listed above \ud83d\ude42<\/font><\/p>\n<p>&#160;<\/p>\n<p><font size=\"3\" face=\"Calibri\"><\/font><\/p>\n<p><font size=\"3\" face=\"Calibri\">One argument I\u2019ve heard for using fields over properties is that \u201cfields are faster\u201d, but for trivial properties that\u2019s actually not true, as the CLR\u2019s Just-In-Time (JIT) compiler will inline the property access and generate code that\u2019s as efficient as accessing a field directly.<\/font><\/p>\n<p><font size=\"3\" face=\"Calibri\">For these reasons and more, in most cases you\u2019re better off using a property rather than exposing a public field. Thankfully <a href=\"http:\/\/code.msdn.microsoft.com\/vbfuture\/Release\/ProjectReleases.aspx?ReleaseId=1699\">VB10<\/a> will make this a lot easier \ud83d\ude42<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day I got an email from a developer asking an interesting question about VB10\u2019s Auto-implemented Properties feature. \u201cI am, however, somewhat confused regarding the properties feature you mentioned &#8211; i.e. turning 11 lines of get\/set\/backing variable into one line. Wouldn&#8217;t this be exactly the same as the existing functionality of creating a public [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[83,167],"class_list":["post-1573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-jonathan-aneja","tag-vb2010"],"acf":[],"blog_post_summary":"<p>The other day I got an email from a developer asking an interesting question about VB10\u2019s Auto-implemented Properties feature. \u201cI am, however, somewhat confused regarding the properties feature you mentioned &#8211; i.e. turning 11 lines of get\/set\/backing variable into one line. Wouldn&#8217;t this be exactly the same as the existing functionality of creating a public [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/1573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=1573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/1573\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=1573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=1573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=1573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}