tags:

views:

110

answers:

8

I noticed that some developers use this syntax for properties :

private int someVar;
public int SomeVar
        {
            get { return someVar; }
            set { someVar= value; }
        }

while some developers use this:

public int SomeVar
        {
            get;
            set;
        }

i am guessing both will be same performance wise. For readibility, reusability and for other factors that you might know, which one is better and considered the way to use inside the community.

+5  A: 

If you aren't going to be doing any validation or anything else when a property is accessed or changed, use the auto property (your second example). If you need to validate your setter, or call a method when someone gets or sets the value, or something of that nature, use your first example. An auto property just prevents you from having both a property and a backing field if you don't need both.

nasufara
and auto properties comes in c# 3.0 and later.
Adeel
+1  A: 

The later version is more readable. And avoids the missuses of "someVar" or "SomeVar"

Numenor
+1  A: 

They are really the same when it comes down to it. For auto-implemented properties the compiler creates the field for you. If you do not need a field because it is a standard getter or setter most people us auto-implemented properties. If you need logic in your properties than you need a field.

Wix
+1  A: 

It depends...

If all you are doing is setting and reading back the value of a field then the second method is preferred as it is considered more readable. This mechanism was introduced in C# 3.0.

If your getter needs to do some validating on the value or your setter needs to fire a PropertyChanged event so that the UI can update then the first method is required.

ChrisF
+1  A: 

As everyone else has said, the latter is called auto property that generates a field automatically at compile time. One extra thing to note is that the exact equivalent of what you have is

public int SomeVar
    {
        get;
        private set;
    }
JeffN825
@Jeff, there's nothing about his code that indicates a private set. It's a public property with no modifiers on the get/set, which makes both of those public, as well.
Anthony Pegram
good point, didn't notice the setter
JeffN825
A: 

Later one is an automatic property , it's like syntactic sugar for creating properties and very handy but under the hood both the representation of properties are same,

saurabh
A: 

The basic and the best way to define a property is like this

private int _student_rollnumber;
private string _student_name;
public int Student_RollNumber
{
 get { return _student_rollnumber; }
 set { _student_rollnumber = value; }
}
public string Student_Name
{
 get { return _student_name; }
 set { _student_name = value; }
 }
programmer4programming
Why is this the best way?
Emile
i personally do not like this for readability since it uses "_" alot.
Numenor
Because i have seen many developers used the same pattern .So i thought this is the best way.
programmer4programming
for private variables uses "_".
programmer4programming
@programmer, C# developers also use pascal/camel casing to break words, not underscores. So Student_Name would simply be StudentName and the backing variable would be studentName or _studentName, depending upon your preference. But with C# 3+, for such trivial get and set operations, the auto-implemented property is the cleanest approach. `public string StudentName { get; set; }`
Anthony Pegram
Ok.Thanks for your valuable info
programmer4programming
A: 

Method 1

private int someVar;
public int SomeVar
    {
        get { return someVar; }
        set { someVar= value; }
    }

This method is generally, not preferred because some languages (such as VB.NET) are case insensitive. Therefore someVar, SomeVar, SOMEVAR and somevar as well as all possible combinations of upper and lower case you can think of mean the same to the compiler and this is likely to generate errors.


Method 2

public int SomeVar{get; set;}

This method, called automatic property implementation, creates a private variable in the shadows for storage and retrieval of the data passed to the property.

In VB.NET, the name of the variable created for each auto-implemented property is the same as the property prefixed with an underscore (_). So a property with name SomeProperty will have a corresponding private variable called _SomeProperty.

This can be demonstrated in your VB.NET code by creating any auto-implemented property and then creating a variable with the same name as the auto-implemented property prefixed with an underscore.

In C#, however, if you have an auto-implemented property, a variable decorated with the CompilerGenerated attribute is used for the property. Thus, you can have the auto-implemented property and a variable with the same name as the property but in a different casing (as in Method 1).


Conclusion
It is generally preferred to use auto-implemented properties whenever possible. Where the need arises that you perform some validation before assigning the property to it's corresponding variable, it is recommended to use name the variable to which the property's value is stored with an underscore prefix as below.

private int _SomeVar;
public int SomeVar
{
    get { return _SomeVar;}
    set { _SomeVar = value > 0 ? value : 0; }
}
Alex Essilfie