Tuesday, May 3, 2016

Inlined if-else assignment

This if-else statement:
AssignmentEnum assignment;
if (loggedUser == onboard.Employee)
    assignment = AssignmentEnum.Employee;
else if (loggedUser == onboard.CreatedBy)
    assignment = AssignmentEnum.Manager;
else
    assignment = 0;

Can be shortened to:
AssignmentEnum assignment = 
    loggedUser == onboard.Employee ?
        AssignmentEnum.Employee
    :loggedUser == onboard.CreatedBy ?
        AssignmentEnum.Manager 
    :
        0;


Happy Coding!