tags:

views:

51

answers:

4

can somebody tell me the difference between

public class Vendor
{
    public string VendorName { get; set; }
}

and

public class Vendor
{
    private string vendorName = string.Empty; 

    public string VendorName
    {
        get { return vendorName; }
        set { vendorName = value; }
    }
}

Is there any benefit of using a private variable? Doing so is only wastage of time and lines? There are no manipulations done to the property within the class.

Thanks

+2  A: 

None. I have found that the auto-propeties cleans up the code quite nicely; especially in classes with larger properties.

EDIT

One think to keep in mind is that if you use a value to specify nulls (i.e. when persisting to a DB; if a DateTime equals DateTime.MinValue, save a NULL to the database), you'll need to declare this in the constructor.

Sample example

instead of

public class Vendor
{
    private DateTime deletedDate = DateTime.MinValue; 

    public DateTime DeletedDate
    {
        get { return deletedDate ; }
        set { deletedDate = value; }
    }
}

you'd need something like this:

public class Vendor
{
    public DateTime DeletedDate { get; set; }

    public Vendor()
    {
        DeletedDate = DateTime.MinValue;
    }
}
Jim B
Thanks Jim. This information is useful.
Kavinda Gayashan
+5  A: 

If this is all you are doing, there is no benefit, IMO.

The significant difference between these blocks of code is that the first defaults to null and the second defaults to string.Empty.

Other than that, I would choose auto properties (option 1) every time. It is something that was added in C# 3.0.

Brian Genisio
+1  A: 

Given your samples, the two are semantically the same (other than the starting value of null for the first and string.Empty for the second, and the fact that the backing variable is not visible to your class in the auto property version). So, no, there is no specific benefit to using a fully-declared property, other than the ability to set an initial value in the declaration (as you do in the example).

When possible, I always prefer auto properties because of the reduction in redundant code (which is why they're there, after all). It's easy enough to set a default value in the constructor, if one is needed.

Adam Robinson
+1  A: 

C# 3 introduced auto-implemented properties, which makes your first snippet possible. The compiler is going to create your backing field for you, which would make the compiled code equivalent to the compiled code of your second snippet. If you're working in a <= 2.0 code base, you'll be using the second snippet.

The benefit of using a private variable would be if you performed some sort of validation when the property is set, such as restricting integers to positive values or rejecting null strings. If you do not need such behavior or simply wish to handle those activities elsewhere and the property is just a fetch and set, then use the auto-implemented version.

Anthony Pegram
Thanks Anthony. I didn't know how auto properties work.
Kavinda Gayashan