More on properties vs fields

Posted by: Eric Gunnersons C# Compendium, on 02 Feb 2007 | View original | Bookmarked: 0 time(s)

Some nice comments on what I wrote.

First, a non-controversial question. Robin asked whether you would capitalize them in that case, and I think you should, as having something accessed externally that isn't properly cased will be surprising to people.

Dani - and others - have pointed out that properties leave your options open in case the software is used in ways that make the property more useful. This is certainly true, and I think it comes down to how you value that flexibility over the tax that you're paying to have properties. I would usually rate the simplicity higher, but it does depend on what you're writing, how likely the code is to get repurposed, and how likely it is that a property would ever be needed. I find as I get older I'm valuing simplicity more.

Finally, Kristof points out that a field can be passed as an out or ref parameter while a property cannot, which means that adding a property to code can break the client code. This is a good point. I do think, however, that I'd want my code to break in that situation - if something is a property I should do some thinking about whether it's a good idea to be passing it as an out or ref (the same reason C# doesn't write helper code to make this work by default).

Kristof's comment reminded me one more reason to use properties - some of the BCL infrastructure works with properties but not with fields.

 

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 771 | Hits: 84

Similar Posts

  • Using T4 Templates for Simple DTOs more
  • Dissecting Lucene.net storage: Documents and Fields more
  • ASP.NET 4.0 Dynamic Data and Many to Many Entity Framework Entities more
  • Four Little Known, Helpful Methods, Properties, and Features for ASP.NET Developers more
  • My History of Visual Studio (Part 10, final) more
  • My History of Visual Studio (Part 9) more
  • Visual Studio Add-In vs. Integration Package Part 3 more
  • Development Tools Ecosystem Summit more
  • Visual Studio Add-In vs. Integration Package Part 2 more
  • Chat room questions from the EF Tips & Tricks webcast more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD