Hi there
I am new for this forum, I have a little question here, pleaseclear my doubt, what is the advantages of properties(get,set), when we go forproperties? why we go for properties?,what is the difference between with usingproperties and without using properties?, because i searched in many link , iasked many people i could not get proper answer kindly clear my doubts, thanks in advance
I am new for this forum, I have a little question here, pleaseclear my doubt, what is the advantages of properties(get,set), when we go forproperties? why we go for properties?,what is the difference between with usingproperties and without using properties?, because i searched in many link , iasked many people i could not get proper answer kindly clear my doubts, thanks in advance
VulpesPosted Nov 13, 2014, 6:26 AM
In fact, it would be sensible to write a method so that you didn't have to write the same validation code every time.
In effect, that's what a property does - the validation code is encapsulated in its 'setter' method.
Besides, properties can do more things than validation. They can, for instance, perform calculations on an object's fields, relieving you of the need to code these calculations every time you need to perform them.
For example, if you had a Rectangle class with height and width fields, you could include a 'read only' Area property in the class, which calculated the area of the rectangle:
class Rectangle
{
double _height;
double _width;
public double Area
{
get { return _height * _width; }
}
// other code
}
kalith rahmanPosted Nov 12, 2014, 11:56 PM
VulpesPosted Nov 11, 2014, 9:11 AM
If someone attempts to give the field an unsuitable value, then this can be caught and the appropriate action taken.
For this reason properties are sometimes called 'smart fields'.
Consider this program:
using System;
class Person
{
private string _name;
private int _age;
public string Name
{
get {return _name;}
set
{
if (String.IsNullOrEmpty(value))
{
Console.WriteLine("Name can't be null or empty");
}
else
{
_name = value;
}
}
}
public int Age
{
get {return _age;}
set
{
if (value < 0)
{
Console.WriteLine("Age can't be negative");
}
else if (value > 122)
{
Console.WriteLine("No one has ever lived to more than 122");
}
else
{
_age = value;
}
}
}
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
class Program
{
static void Main()
{
Person p = new Person("", -4);
p.Name = "Fred";
p.Age = 130;
p.Age = 21;
Console.WriteLine("{0}'s age is {1}", p.Name, p.Age);
Console.ReadKey();
}
}
The output is:
Name can't be null or empty
Age can't be negative
No one has ever lived to more than 122
Fred's age is 21
Here, we're doing what we can to ensure that the _name and _age fields of the Person class can't be set to daft values by wrapping them in the Name and Age properties which provide some checks.
Of course, nothing's perfect and, as the code stands, there's nothing to stop you naming a person Zzyyyxx even though it's unlikely to be a name in any language :)