If I nullify the value in the case of collection in the called function,it is not affecting in the calling method.It is still having the original values..Why the value is not null in the calling function..?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Dipa AhujaRank #1,168 · 1.5K reputationPosted May 26, 2010, 2:30 AM EDT
Dipankar Sana26 reputationPosted May 26, 2010, 2:54 AM EDT
I have created one arraylist.
ArrayList arr=new ArrayList();
Arr.Add(1);
SomeMethod(Arr);
Public ArrayList SomeMethod(ArrayList Arrs)
{
Arrs.Add(2);
return Arrs;
}
In the above case the updated result is getting reflected in the variable Arr in calling method i.e now we are getting two items in Arr.
But if in SomeMethod function we nullify Arrs and return Arrs.its not getting updated with null value in calling function .Still it is having old value..i.e 1.
Dipa AhujaRank #1,168 · 1.5K reputationPosted May 26, 2010, 3:15 AM EDT
Dipankar Sana26 reputationPosted May 26, 2010, 3:20 AM EDT
Why the null value is not reflecting whereas modifications are getting reflected ??
Dipa AhujaRank #1,168 · 1.5K reputationPosted May 26, 2010, 3:23 AM EDT
Dipankar Sana26 reputationPosted May 26, 2010, 3:37 AM EDT
But I just wanted to know why it was behaving like that when not adding ref.. ?
Jaish MathewsRank #225 · 11K reputationPosted May 26, 2010, 3:46 AM EDT
What understood is that you are asking why ArrayList not null even after made it null in side another function. Actually it's null only, but you are not storing that value and seems like it's not null. Look below code and concentrate on bolded lines where I stored the latest version after nullified. Here I am getting null
private void Form1_Load(object sender, EventArgs e)
{
ArrayList arr = new ArrayList();
arr.Add(1);
arr = SomeMethod(arr);
}
public ArrayList SomeMethod(ArrayList arr)
{
arr = null;
return arr;
}
Dipa AhujaRank #1,168 · 1.5K reputationPosted May 26, 2010, 3:47 AM EDT