initialization of array from sub function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

I'm not sure if I understeand the garbage collector. Is this code legal, or
will days well destroyed when this function exits:

private int[] GetDays()
int[] days = new int[2];
days[0] = 0;
days[1] = 30;
return days;
}

private void GetVehicleList()
{
int[] days = GetDays();
 
Wilfried Mestdagh said:
Hello

I'm not sure if I understeand the garbage collector. Is this code legal, or
will days well destroyed when this function exits:

private int[] GetDays()
int[] days = new int[2];
days[0] = 0;
days[1] = 30;
return days;
}

private void GetVehicleList()
{
int[] days = GetDays();

Nope it will stay alive as long as you have a reachable reference to it.

Since days is such a reference....you are OK.

Bill
 
Back
Top