如何在C#中使用变量值作为类实例的名称?
如何在C#中使用变量值作为类实例的名称?
嘿,我完全懂你想实现的需求——让每个新创建的员工实例,能直接用用户输入的姓名来“指代”它,但在C#里没法直接用变量值作为类实例的名称哦!因为C#是静态类型语言,所有变量名(也就是标识符)在编译阶段就必须确定,没办法在运行时动态生成新的变量名。不过别担心,我们有更合适的方案来达成你的目标:用一个Dictionary(字典)来存储所有员工实例,把员工的姓名组合作为字典的“键”,这样就能通过姓名快速找到对应的员工对象,完美满足你“无限创建唯一姓名员工”的要求。
具体修改方案
首先,在Main方法里先声明一个字典,用来存员工:
// 用来存储员工,键是员工的姓名组合,值是Employee实例 Dictionary<string, Employee> employees = new Dictionary<string, Employee>();
然后把你原来case "A"里的错误代码,改成添加员工到字典的逻辑,还要加个重名检查:
case "A": // Create new employee Console.WriteLine("Enter new employee's first name:"); firstName = Console.ReadLine(); Console.WriteLine("Enter new employee's last name:"); lastName = Console.ReadLine(); Console.WriteLine("Enter new employee's email address:"); emailAddress = Console.ReadLine(); Console.WriteLine("Enter new employee's age:"); age = Convert.ToInt32(Console.ReadLine()); // 生成唯一键(加下划线分隔名和姓,避免类似"JohnDoe"和"JonhDoe"的混淆) string employeeKey = $"{firstName}_{lastName}"; if (employees.ContainsKey(employeeKey)) { Console.WriteLine("这个姓名的员工已经存在啦,请换一个!"); } else { Employee newEmployee = new Employee(firstName, lastName, emailAddress, age); employees.Add(employeeKey, newEmployee); Console.WriteLine($"员工 {firstName} {lastName} 已成功创建!"); } break;
扩展功能示例
既然用了字典,我们还能轻松实现你菜单里的删除、查看功能:
case "B": Console.WriteLine("Enter the first name of the employee to remove:"); string removeFirstName = Console.ReadLine(); Console.WriteLine("Enter the last name of the employee to remove:"); string removeLastName = Console.ReadLine(); string removeKey = $"{removeFirstName}_{removeLastName}"; if (employees.Remove(removeKey)) { Console.WriteLine($"员工 {removeFirstName} {removeLastName} 已删除!"); } else { Console.WriteLine("找不到这个员工,无法删除哦!"); } break; case "C": Console.WriteLine("Enter the first name of the employee to check:"); string checkFirstName = Console.ReadLine(); Console.WriteLine("Enter the last name of the employee to check:"); string checkLastName = Console.ReadLine(); string checkKey = $"{checkFirstName}_{checkLastName}"; if (employees.TryGetValue(checkKey, out Employee targetEmployee)) { targetEmployee.ListEmployeeDetails(); } else { Console.WriteLine("找不到这个员工的信息!"); } break; case "E": next = false; Console.WriteLine("感谢使用,再见!"); break;
完整修改后的代码
using System; using System.Collections.Generic; public class Employee { public string firstName; public string lastName; public string emailAddress; public int age; public Employee(string name1, string name2, string email, int age1) { firstName = name1; lastName = name2; emailAddress = email; age = age1; } public void ListEmployeeDetails() { Console.WriteLine("--Employee details--"); Console.WriteLine("~~~~~~~~~~~~~~~~~~~~"); Console.WriteLine($"Name: {firstName} {lastName}"); Console.WriteLine($"Email: {emailAddress}"); Console.WriteLine($"Age: {age}"); } } public class Program { public static void Main(string[] args) { // Setup string decision; string firstName; string lastName; string emailAddress; int age; bool next = true; Random rnd = new Random(); int num = rnd.Next(1, 201); // 新增:存储所有员工的字典 Dictionary<string, Employee> employees = new Dictionary<string, Employee>(); // Explanation Console.WriteLine("Welcome to Employee console simulator. \nWould you like to create a new employee, remove an employee, or check an employee's work hours?\nPress 'a' to create new employee, 'b' to remove an employee, and 'c' to check an employee's work hours.\nPress 'e' to exit."); while (next == true) { decision = Console.ReadLine().ToUpper(); switch (decision) { case "A": Console.WriteLine("Enter new employee's first name:"); firstName = Console.ReadLine(); Console.WriteLine("Enter new employee's last name:"); lastName = Console.ReadLine(); Console.WriteLine("Enter new employee's email address:"); emailAddress = Console.ReadLine(); Console.WriteLine("Enter new employee's age:"); age = Convert.ToInt32(Console.ReadLine()); string employeeKey = $"{firstName}_{lastName}"; if (employees.ContainsKey(employeeKey)) { Console.WriteLine("This employee already exists! Please enter a different name."); } else { Employee newEmployee = new Employee(firstName, lastName, emailAddress, age); employees.Add(employeeKey, newEmployee); Console.WriteLine($"Employee {firstName} {lastName} created successfully!"); } break; case "B": Console.WriteLine("Enter the first name of the employee to remove:"); string removeFirstName = Console.ReadLine(); Console.WriteLine("Enter the last name of the employee to remove:"); string removeLastName = Console.ReadLine(); string removeKey = $"{removeFirstName}_{removeLastName}"; if (employees.Remove(removeKey)) { Console.WriteLine($"Employee {removeFirstName} {removeLastName} removed successfully!"); } else { Console.WriteLine("Employee not found, cannot remove!"); } break; case "C": Console.WriteLine("Enter the first name of the employee to check:"); string checkFirstName = Console.ReadLine(); Console.WriteLine("Enter the last name of the employee to check:"); string checkLastName = Console.ReadLine(); string checkKey = $"{checkFirstName}_{checkLastName}"; if (employees.TryGetValue(checkKey, out Employee targetEmployee)) { targetEmployee.ListEmployeeDetails(); } else { Console.WriteLine("Employee not found!"); } break; case "E": next = false; Console.WriteLine("Thank you for using the simulator, goodbye!"); break; default: Console.WriteLine("That is not a valid command. Please enter again."); break; } } } }
这样修改后,你不仅能无限创建唯一姓名的员工,还能方便地进行删除和查询操作,完全满足你的需求啦!
备注:内容来源于stack exchange,提问作者interblade




