Was just reading this post by Jeremy Miller (via Keith) and some of the code in it has forced me to write this post, completely unrelated to the topic of Jeremy's post.
So often I see people writing properties that look like this:
public DateTime InvoiceDate
{
get { return _invoiceDate; }
set { _invoiceDate = value; }
}
public string InvoiceNumber
{
get { return _invoiceNumber; }
set { _invoiceNumber = value; }
}
Damnit people, C#'s has its properties so that we don't have to go and write accessors for all public properties like people do in Java. Make it a public data member and be done with it until you actually need custom behavior. Otherwise you're just needlessly writing code around what C# already does.
Update: There are actually two cases where you would want to define "unnecessary" properties such as these. Adam explains the case of Winforms Data Binding:
Properties and member fields are treated differently by System.Reflection. You can databind to an object property, but not a plan old member.
...
The sad thing is that both MemberInfo and PropertyInfo, the classes that encapsulate field and property access, both have "GetValue" and "SetValue" members, but they're not defined in the base class nor with a common interface, so you can't use these interchangeably.
The other case is if you're writing code specifically to be distributed as a dll. Even though the syntax is the same, the generated code is of course different, and changing from a member to a property would require a recompile of the client code.
new⇒I hate ASP.NET
CF, why pick that piece of trash?Cold Confusion. Is it finallyreally a OO...
ColdConfusion: Sep 5, 8:36pm