Kavinda Gayashan
Kavinda Gayashan

Reputation: 387

Defining properties of a class

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

Upvotes: 1

Views: 417

Answers (4)

Anthony Pegram
Anthony Pegram

Reputation: 126794

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.

Upvotes: 2

Adam Robinson
Adam Robinson

Reputation: 185583

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.

Upvotes: 1

Brian Genisio
Brian Genisio

Reputation: 48127

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.

Upvotes: 5

Jim B
Jim B

Reputation: 8574

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;
    }
}

Upvotes: 2

Related Questions