Exercise: Employee Directory Formatter
Problem statement
HR systems often need to export employee lists to console logs or text files. By default, attempting to print a custom object outputs its fully qualified type name, which is useless to a human reader. The objective is to ensure that whenever an employee is printed, it outputs a cleanly formatted and readable string.
Task requirements
Create an
Employeeclass withFirstName,LastName, andDepartmentproperties.Override the default string conversion behavior.
Format the returned string exactly as:
LastName, FirstName (Department).
Constraints
Use the
overridekeyword to modify the virtualToString()method inherited implicitly fromSystem.Object.Place the
Employeeclass in theHRSystemsnamespace within its own file.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Every class in C# automatically inherits from
System.Object.The
System.Objectclass provides apublic virtual string ToString()method.You must use the exact signature
public override string ToString()to replace the default behavior.
Exercise: Employee Directory Formatter
Problem statement
HR systems often need to export employee lists to console logs or text files. By default, attempting to print a custom object outputs its fully qualified type name, which is useless to a human reader. The objective is to ensure that whenever an employee is printed, it outputs a cleanly formatted and readable string.
Task requirements
Create an
Employeeclass withFirstName,LastName, andDepartmentproperties.Override the default string conversion behavior.
Format the returned string exactly as:
LastName, FirstName (Department).
Constraints
Use the
overridekeyword to modify the virtualToString()method inherited implicitly fromSystem.Object.Place the
Employeeclass in theHRSystemsnamespace within its own file.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Every class in C# automatically inherits from
System.Object.The
System.Objectclass provides apublic virtual string ToString()method.You must use the exact signature
public override string ToString()to replace the default behavior.