C# 为什么不从employees数组中删除my employee对象?(不要介意我的方法)
将变量设置为C# 为什么不从employees数组中删除my employee对象?(不要介意我的方法),c#,C#,将变量设置为null不会更改引用/使用对象的任何其他位置。请参见以下示例: if (humanResourceManager.IsEmployeeExistByDepartmentNameAndNo(depname, employeeNo)) { Employee employee = humanResourceManager.GetEmployeeExistByNo(employeeNo); Employe
null
不会更改引用/使用对象的任何其他位置。请参见以下示例:
if (humanResourceManager.IsEmployeeExistByDepartmentNameAndNo(depname, employeeNo))
{
Employee employee = humanResourceManager.GetEmployeeExistByNo(employeeNo);
Employee[] employees = humanResourceManager.GetDepartmentByName(depname).Employees;
employee = null;
Array.Sort(employees);
Array.Reverse(employees);
while (employees[employees.Length - 1] == null)
{
Array.Resize(ref employees, employees.Length - 1);
}
Console.WriteLine("Silindi!");
}
这不会更改变量s
,它仍然引用字符串对象“abc”
(但变量c
现在的值为null
)。阵列也是如此:
string s = "abc";
string c = s;
string c = null;
同样,您只更改变量c
,但不更改数组数据
。当您要更改数组时,必须使用数据
数组,如下所示:
string[] data = new string[] {"abc", "def", "ghi"};
string c = data[1];
string c = null;
在您的情况下,必须更改
Employee[]employees
数组。根据您的代码,您可能需要使用humanResourceManager
中的方法来更新数组来自的源数据。Employee[]employees=humanResourceManager.GetDepartmentByName(depname).employees.where(z=>z!=Employee.ToArray()代码>
string[] data = new string[] {"abc", "def", "ghi"};
data[1] = null;
// the array is now: ["abc", null, "ghi"]