I know the “logical approach” to this question …
This is logically correct:
public static Department FindDepartment(Employee emp)
{
if(emp.ID > 500)
{
return new Department("Department for Over 500");
}else{
return new Department("Department for 500 and under");
}
}
… because the Department is really based off the Employee, whereas the following is logically incorrect:
public static int GetPlusOne(Employee emp)
{
return emp.ID + 1;
}
… because the method really isn’t a function of an Employee … it’s just an integer modifier.
That all said (and feel free to point out if I’m in error on that for some reason), Is there any performance lost when passing the entire instance of Employee?
I have an underlying method that will be called most of the time from within a different class and so I’m trying to weigh performance vs. logical here. But if there’s nothing to be worried about performance-wise then the choice becomes logical (haha … punny).
IF
Employeeis aclassthen .NET passes a reference (which is basically a pointer) and NOT the instance itself… so performance is rather similar to a real instance method (which too receives a reference to the instance as an implicit parameter calledthis).You can even define them as Extension Methods like:
then you call it like any instance method on an instance of
Employee: