Exercise: Employee Directory Formatter

Format custom object outputs by overriding the virtual string method from the base object class.

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 Employee class with FirstName, LastName, and Department properties.

  • Override the default string conversion behavior.

  • Format the returned string exactly as: LastName, FirstName (Department).

Constraints

  • Use the override keyword to modify the virtual ToString() method inherited implicitly from System.Object.

  • Place the Employee class in the HRSystems namespace 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.Object class provides a public virtual string ToString() method.

  • You must use the exact signature public override string ToString() to replace the default behavior.

Exercise: Employee Directory Formatter

Format custom object outputs by overriding the virtual string method from the base object class.

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 Employee class with FirstName, LastName, and Department properties.

  • Override the default string conversion behavior.

  • Format the returned string exactly as: LastName, FirstName (Department).

Constraints

  • Use the override keyword to modify the virtual ToString() method inherited implicitly from System.Object.

  • Place the Employee class in the HRSystems namespace 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.Object class provides a public virtual string ToString() method.

  • You must use the exact signature public override string ToString() to replace the default behavior.

C# 14.0
namespace HRSystems;
// TODO: Define a public class named Employee
// TODO: Add string properties for FirstName, LastName, and Department
// TODO: Override the ToString() method to return the formatted string