Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I saw one of my collegues doing code like this today:

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


Personally, I would have preferred doing:

C#
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.

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".

C#
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.
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 21-Nov-11 4:25am    
And?..
--SA
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:

C#
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[^]
 
Share this answer
 
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 :)
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Nov-11 4:12am    
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.
Sergey Alexandrovich Kryukov 21-Nov-11 4:24am    
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;
}

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900