The ideal solution would be to detect duplicates as soon as possible as the user enters data, or, later, submits a form, or does whatever to indicate they have finished data entry ... before you even create an instance of an 'Employee class. That's called, in general, "Validation."
But, you could use a special constructor that would throw an error if created/initialized with "wrong" values, and you can use Properties with only a 'Get, and private backing-field variables to increase the security around the data fields:
public class Employee
{
private string _ename;
private string _eaddress;
private string _id;
public string ID
{
get { return _id; }
}
public string Ename
{
get { return _ename; }
}
public string Eaddress
{
get { return _eaddress; }
}
public Employee(string id, string name, string address)
{
if(_id == _ename) throw new ArgumentException("Class Employee: ID and Name cannot be the same !");
_id = id;
_ename = name;
_eaddress = address;
}
public void ChangeEmployeeData(string password, string username, string id, string name, string address)
{
if (SecurityCheck(password, username))
{
if (!String.IsNullOrEmpty(id)) _id = id;
if (!String.IsNullOrEmpty(name)) _ename = name;
if (!String.IsNullOrEmpty(address)) _eaddress = address;
}
else
{
}
}
public bool SecurityCheck(string password, string username)
{
return true;
}
}
Notes:
1. why use Properties: to make serialization easier, to make code more self-documenting
2. again, I'd like to stress that the better way is control the input process so the user never gets the chance to enter "wrong" values; however, it is true that in the real-world sometimes you are dealing with inherited data that you have no control over.