Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, August 29, 2014 11:47 PM
How can I pass a list by reference into a method? Below is a method, and I want to pass my list by reference. I tried putting the ref keyword in my method call, but that is not working.
List<FoodOrder> FoodsOrderList = null; // This is the first list being passed.
getTheFoodOrder(2, ref FoodsOrderList); // Pass my list by reference to the method "getTheFoodOrder"
List<FoodOrder> DryGoodsOrderList = null; // This is the second list being passed.
getTheFoodOrder(2, ref DryGoodsOrderList); // Pass my list by reference to the method "getTheFoodOrder"
public void getTheFoodOrder(int Player, ref FoodsOrderList)
{
FoodsOrderList.Clear();
FoodOrder o = new FoodOrder();
o.FoodType = 250;
o.Count = Player;
FoodsOrderList.Add(o);
FoodOrder o = new FoodOrder();
o.FoodType = 350;
o.Count = Player;
FoodsOrderList.Add(o);
}
class FoodOrder : IComparable<FoodOrder>
{
private int _Count;
private int _FoodType;
private int _OrderIndex;
public int CompareTo(FoodOrder ediblesCount)
{
return this.Count.CompareTo(ediblesCount.Count);
}
public int Count
{
get { return _Count; }
set { _Count = value; }
}
public int FoodType
{
get { return _FoodType; }
set { _FoodType = value; }
}
}
All replies (9)
Monday, September 1, 2014 7:08 AM ✅Answered
Hi AppDevForMe,
Please refer to the following code:
protected void Button1_Click(object sender, EventArgs e)
{
List<FoodOrder> FoodsOrderList = null; // This is the first list being passed.
getTheFoodOrder(2, ref FoodsOrderList); // Pass my list by reference to the method "getTheFoodOrder"
Response.Write(FoodsOrderList[1].Count + " " + FoodsOrderList[1].FoodType);
Response.Write("<br />*********************<br />");
List<FoodOrder> DryGoodsOrderList = null; // This is the second list being passed.
getTheFoodOrder(2, ref DryGoodsOrderList); // Pass my list by reference to the method "getTheFoodOrder"
Response.Write(DryGoodsOrderList[1].Count + " " + DryGoodsOrderList[1].FoodType);
}
public static void getTheFoodOrder(int Player, ref List<FoodOrder> refList)
{
List<FoodOrder> list = new List<FoodOrder>();
FoodOrder o = new FoodOrder();
o.FoodType = 250;
o.Count = Player;
list.Add(o);
FoodOrder o2 = new FoodOrder();
o2.FoodType = 350;
o2.Count = Player;
list.Add(o2);
refList = list;
}
More details about ref (C# Reference), please refer to the following link: http://msdn.microsoft.com/en-us/library/14akc2c7.aspx
If you have any further questions, please feel free to post in this forum.
Best Regards,
Dillion
Wednesday, September 3, 2014 7:47 AM ✅Answered
If you are passing a parameter by reference then it must be set to a non-null value before calling the method.
This is not correct. You do need to initialize the variable that you are intending to pass using a ref keyword by first assigning to it , but it is acceptable to assign null.
Also should point out that there's an additional issue with the originally posted code sample that would prevent it from compiling.
This method has not provided a Type for the 2nd parameter:
public void getTheFoodOrder(int Player, ref FoodsOrderList)
Adding a Type would look like this:
public void getTheFoodOrder(int Player, ref List<FoodOrder> FoodsOrderList)
And if we were all to agree that the ref keyword wasn't needed then maybe just use this:
public void getTheFoodOrder(int Player, List<FoodOrder> FoodsOrderList)
Lastly, I think the confusion around the ref keyword and the signature of that method revolves around the original code sample trying to use a method parameter as a return mechanism. Why not just return from the method normally instead of making a void method and returning the answer through the method parameter?
Why not just write the method like this:
public List<FoodOrder> getTheFoodOrder(int Player)
{
List<FoodOrder> FoodsOrderList = new List<FoodOrder>();
//FoodsOrderList.Clear();
FoodOrder o = new FoodOrder();
o.FoodType = 250;
o.Count = Player;
FoodsOrderList.Add(o);
FoodOrder o = new FoodOrder();
o.FoodType = 350;
o.Count = Player;
FoodsOrderList.Add(o);
return FoodsOrderList;
}
I think the above is more clear in its intent and calling it is simpler too:
List<FoodOrder> FoodsOrderList = getTheFoodOrder(2);
Saturday, August 30, 2014 12:58 AM
The list is passed by reference by default. Just pass it "normally".
Saturday, August 30, 2014 12:59 AM
class Program
{
static void Main(string[] args)
{
List<int> listA = new List<int> { 1, 2, 3 };
List<int> listB = new List<int> { 1, 2, 3 };
Update(listA);
UpdateRef(ref listB);
Console.WriteLine("listA");
foreach (var val in listA)
Console.WriteLine(val);
Console.WriteLine("listB");
foreach (var val in listB)
Console.WriteLine(val);
}
static void Update(List<int> list)
{
list = new List<int>() { 4, 5, 6 };
}
static void UpdateRef(ref List<int> list)
{
list = new List<int>() { 4, 5, 6 };
}
}
here is some sample code which i did..i did it with console base..u can move it to web base..please check that out
Tuesday, September 2, 2014 10:28 PM
I tried putting the ref keyword in my method call, but that is not working.
Given the code sample you've provided, that code should throw an error (null reference exception) regardless of whether you'd used the ref keyword or not.
Right before you call getTheFoodOrder, you are defining your FoodsOrderList variable
List<FoodOrder> FoodsOrderList = null;
Notice that you've set the variable to null.
The very first line of the method getTheFoodOrder tries to clear the FoodsOrderList that was passed
FoodsOrderList.Clear();
Since a null was passed, this line should Throw a null reference exception.
As MetalAsp.Net correctly pointed out, when passing a reference type such as a list, you just pass it normally - no ref keyword needed.
But, you do need to create an actual instance of a list before passing it rather than leaving it as null
So this:
List<FoodOrder> FoodsOrderList = null;
becomes this:
List<FoodOrder> FoodsOrderList = new List<FoodOrder>();
Then drop the ref keywords on the method and the method call and you should be good to go.
Now it is possible to use the ref keyword with a reference type such as a list and doing so allows for slightly different behavior than without the ref keyword. But unless you have a very specific reason for wanting to do so, you might want to avoid it. based on your sample code, i don't think you need it but you can read a little bit about it here: http://stackoverflow.com/questions/4802902/pass-reference-variable-by-ref-or-by-val/4802938#4802938.
Wednesday, September 3, 2014 12:56 AM
If you are passing a parameter by reference then it must be set to a non-null or null value i.e. it must be initialized So on that part your code is ok.
But as pointed out by others before me, calling Clear and Add methods on the passed list will throw an exception since you cannot call object methods on an object that is null. So, to get around this, you can pass a non-null and empty list as in code below to your methods.
List<FoodOrder> FoodsOrderList = new List<FoodOrder>(); // This is the first list being passed.
getTheFoodOrder(2, ref FoodsOrderList); // Pass my list by reference to the method "getTheFoodOrder"
List<FoodOrder> DryGoodsOrderList = new List<FoodOrder>; // This is the second list being passed.
getTheFoodOrder(2, ref DryGoodsOrderList); // Pass my list by reference to the method "getTheFoodOrder"
You can read more about passing objects by ref on MSDN @ http://msdn.microsoft.com/en-us/library/14akc2c7.aspx.
The following are some useful paragraphs taken from above MSDN location.
"An argument that is passed to a ref parameter must be initialized before it is passed. This differs from out parameters, whose arguments do not have to be explicitly initialized before they are passed."
"Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference."
Wednesday, September 3, 2014 1:04 AM
List<FoodOrder> DryGoodsOrderList = nullnew List<FoodOrder>; // This is the second list being passed.
Besides stating what had already been started several hours earlier by mbanavige, you used nullnew. What is that?
Wednesday, September 3, 2014 1:12 AM
That was a typo which I have corrected. Thanks for pointing it out since it missed me completely and would have confused the reader of my answer.
Wednesday, September 3, 2014 9:38 AM
You are right.