As you can see under I have create getter and setter for class property. I'm wondering, is it fine to set it up like I did under? I didn't use the private variable in the get, is it fine or should I use it? Could someone correct me how my class in this case, should look like, if my approach is wrong? The class is CloneAble
public int? Id { get; set; }
public string Code { get; set; }
private string _name;
public string Name
{
get
{
if (this.Id == null)
return null;
else
return this.Id+ "/" + this.Code;
}
set
{
if (this._name != value)
{
this._name= value;
}
}
}
public bool IsValid
{
get
{
if (this.Id == null)
return false;
else
return true;
}
}
The class is Cloneable:
public object Clone()
{
var elem= new Element();
this.SetPropertyValues(elem);
return elem;
}
private void SetPropertyValues(elem)
{
var propertyInfo = this.GetType().GetProperties().Where(p => p.CanWrite && (p.PropertyType.IsValueType || p.PropertyType.IsEnum || p.PropertyType.Equals(typeof(System.String))));
foreach (PropertyInfo property in propertyInfo)
{
if (property.CanWrite)
{
property.SetValue(elem, property.GetValue(this, null), null);
}
}
}