Click here to Skip to main content
14,543,626 members
Rate this:
Please Sign up or sign in to vote.
See more:
I saw one of my collegues doing code like this today:

public static void DoStuffWithList(ref List<myclass> result)


Personally, I would have preferred doing:

public static List<myclass> DoStuffWithList(List<control> result)



Actually, if I'm not mistaken, "my" method also takes the list as reference parameter, and you should be able to use it just the same way as my collegue does in his method.

But it seems more "clean" to me that you enter a parameter, do something with it and return it in the methods returnvalue. Could just be that I'm too old, though and have an old way of looking at it...

What do you think? Do you have any good arguments for or against one method or the other?

[EDIT]Had to remove the code tags aound the code, because the CP editor apparently doesn't like List<> and insists on inserting an unwanted tag in the code...[/EDIT]

[EDIT2]Changed Generics angle brackets to unicode[/EDIT2]
Posted
Updated 21-Nov-11 0:28am
v5
Comments
LanFanNinja 21-Nov-11 4:05am
   
Check solution below
Amir Mahfoozi 21-Nov-11 4:08am
   
Hi Johnny,
I think if your idea be converted to an extension method it would be absolutely preferable to your colleagues function, because it makes chain calls to the method possible from the result sets.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Read this

list-passed-by-ref-help-me-explain-this-behaviour[^]

After returning from Test1 lst will still contain "Fred" and "Bob".
After returning from Test2 lst will contain "Ted" and "Ned".

List<string> lst = new List<string>();
lst.Add("Fred");
lst.Add("Bob");

Test1(lst);
Test2(ref lst);

private void Test1(List<string> lst) 
{ 
    List<string> newLst = new List<string>(); 
    newLst.Add("Ted");
    newLst.Add("Ned");
    lst = newLst; 
}

private void Test2(ref List<string> lst)
{
    List<string> newLst = new List<string>();
    newLst.Add("Ted");
    newLst.Add("Ned");
    lst = newLst; 
}


This problem is not noticed a lot because most people use methods like Add(); and AddRange(newLst); to add items to a List.
   
v5
Comments
   
And?..
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 3

It's likely that you don't need to use ref - but there is a difference.

Usually when I see people using ref for reference type parameters, it's because they don't understand how parameter passing works. But if your method has something like this:

result = new List();
...

then in the first case the caller won't see the change, whereas in the second case the caller's variable will be changed to refer to the new object.

check here for more http://www.yoda.arachsys.com/csharp/parameters.html[^]

check this simple example too

http://pastebin.com/sm8niPRD[^]
   
v2
Comments
Johnny J. 21-Nov-11 4:21am
   
That's true. That's actually a good argument for using the reference parameter style instead of my returnvalue style. Hadn't thought that far... :-)
demouser743 21-Nov-11 4:25am
   
Yeah and the best method to choose is as per your requirement.

When your method modifies the list or returns new data you should use the return value. Its much better to understand what your code does than using a ref parameter.

Another benefit of return values is the ability to use method chaining.
Johnny J. 21-Nov-11 4:30am
   
That's also true - another good argument. I'd say it's 1-all now... ;-)
LanFanNinja 21-Nov-11 4:47am
   
+5
demouser743 21-Nov-11 5:01am
   
Lan FanNinja Thanks :)
Johnny J. 21-Nov-11 11:02am
   
Thanks everybody - I'm very pleased with the arguments I got here... Will mark it as solved.
demouser743 21-Nov-11 11:15am
   
Johnny Thanks for that :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

public static void DoStuffWithList(List result) will do what you need as well.
This is would be the best way to right code. Any object is passed by reference by default.

You do not need to return it back to the callee.
   
Comments
   
Absolutely. DoStuffWithList(ref List result) is a sign of a mental disorder. My 5.
--SA
Abhinav S 21-Nov-11 5:34am
   
Thank you SA.
LanFanNinja 21-Nov-11 4:13am
   
I assure you it does not! Check my solution.
   
Well, what's your proof? Do you mean that in the article you referenced somebody said that passing a list by reference makes sense? But how do you proof that this person if free from a mental disorder? No, please give us a rational argument, personally, without references, and we will see...
--SA
LanFanNinja 21-Nov-11 4:32am
   
After returning from Test1 lst will still contain "Fred" and "Bob".
After returning from Test2 lst will contain "Ted" and "Ned".

List<string> lst = new List<string>();
lst.Add("Fred");
lst.Add("Bob");

private void Test1(List<string> lst)
{
List<string> newLst = new List<string>();
newLst.Add("Ted");
newLst.Add("Ned");

lst = newLst;
}

private void Test2(ref List<string> lst)
{
List<string> newLst = new List<string>();
newLst.Add("Ted");
newLst.Add("Ned");

lst = newLst;
}
Nickos_me 21-Nov-11 6:39am
   
In your case - yes. But this case have noе too much sense.

Try this:

private void Test1(List<string> lst)
{
lst.Clear();
lst.Add("Ted");
lst.Add("Ned");
}
LanFanNinja 21-Nov-11 6:52am
   
Yes I know. All I am saying is they are not the same and there may be situations where you would want to pass the List by ref. I am not saying it is good practice or anything else.
[no name] 21-Nov-11 4:14am
   
Absolutely correct. 5ed.
Abhinav S 21-Nov-11 5:34am
   
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100