为什么它不从雇员数组中删除我的雇员对象?(不要介意我的方法)

  • 本文关键字:方法 对象 删除 数组 c#
  • 更新时间 :
  • 英文 :

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!");

}

将变量设置为null不会改变对象被引用/使用的任何其他位置。请看下面的例子:

string s = "abc";
string c = s;
string c = null;

这不会改变变量s,它仍然引用字符串对象"abc"(但变量c现在具有值null)。数组也是如此:

string[] data = new string[] {"abc", "def", "ghi"};
string c = data[1];
string c = null;

同样,您只更改了变量c,但没有更改数组data。当你想改变数组时,你必须使用data数组,像这样:

string[] data = new string[] {"abc", "def", "ghi"};
data[1] = null;
// the array is now: ["abc", null, "ghi"]

在您的情况下,您必须更改Employee[] employees数组。根据您的代码,您可能需要使用humanResourceManager中的方法来更新数组来自何处的源数据。

最新更新