最佳做法问题 [英] Best practise question

查看:100
本文介绍了最佳做法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我看到一位同事在做这样的代码:

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)




实际上,如果我没记错的话,我的"方法也将列表作为参考参数,并且您应该能够像同事在他的方法中一样使用它.

但是对我来说似乎更干净"是,您输入了一个参数,对其进行了处理,然后在方法returnvalue中将其返回.可能只是我太老了,并且有一种老套的看法...

你怎么看?您是否支持或反对一种方法有什么好的论点?

必须删除代码中的代码标签,因为CP编辑器显然不喜欢List<>.并坚持在代码中插入不需要的标签... [/EDIT]

已将泛型尖括号更改为unicode [/EDIT2]




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?

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]

Changed Generics angle brackets to unicode[/EDIT2]

推荐答案

阅读此内容

list-passed-by-ref-help-me-explain-此行为 [ ^ ]

从Test1返回后,lst仍将包含"Fred"和"Bob".
从Test2返回后,lst将包含"Ted"和"Ned".

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; 
}



由于大多数人都使用Add()之类的方法,因此这个问题很少引起注意.和AddRange(newLst);将项目添加到列表中.



This problem is not noticed a lot because most people use methods like Add(); and AddRange(newLst); to add items to a List.


您可能不需要使用ref-但有所不同.

通常,当我看到人们使用ref作为引用类型参数时,这是因为他们不了解参数传递的工作原理.但是,如果您的方法具有以下内容:

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();
...


那么在第一种情况下,调用者将看不到更改,而在第二种情况下,调用者的变量将被更改以引用新对象.

在此处查看更多 http://www.yoda.arachsys.com/csharp/parameters.html [ ^ ]

还要检查这个简单的例子

http://pastebin.com/sm8niPRD [ ^ ]


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[^]


public static void DoStuffWithList(List result)也会满足您的需求.
这是正确编写代码的最佳方法.默认情况下,任何对象都是通过引用传递的.

您无需将其返回给被调用方.
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.


这篇关于最佳做法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆