I used the same TextChanged event handler for 2 textboxs so Is their any performance improvement or it is just matter of readability?
Method 1
private void txtcode_TextChanged(object sender, EventArgs e)
{
//Some code
}
private void txtname_TextChanged(object sender, EventArgs e)
{
//Some code
}
Method 2
private void txt_TextChanged(object sender, EventArgs e)
{
TextBox txtbx = (TextBox)sender;
switch (txtbx.Name)
{
case "txtname":
//Some code
break;
case "txtcode":
//Some code
break;
default:
break;
}
}
Method 1
will be better, as they are direct and knows for that control they are responding too
Regarding
Method 2
each time the sender(object) is casted to TextBox and then the switch statement decides the Operation on the control.
so it is obvious that,
Method1will be more effectiveHave a look at these links too : How expensive is Casting, Performance of Typecasting