Reputation: 41
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.
Upvotes: 4
Views: 203
Reputation: 12613
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; }
}
Upvotes: 0
Reputation: 4611
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; }
}
Upvotes: -1
Reputation: 36573
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;
}
Upvotes: 1
Reputation: 137148
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.
Upvotes: 1
Reputation: 2214
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.
Upvotes: 1
Reputation: 1706
The later version is more readable. And avoids the missuses of "someVar" or "SomeVar"
Upvotes: 1
Reputation:
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.
Upvotes: 5