在C#中,你用与在那里QUOT;参考"在参数的前面? [英] In C#, where do you use "ref" in front of a parameter?

查看:105
本文介绍了在C#中,你用与在那里QUOT;参考"在参数的前面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些已经在裁判和out参数的定义问题,但他们看起来糟糕的设计。在那里,你觉得裁判是正确的解决方案的情况下任何

There are a number of questions already on the definition of "ref" and "out" parameter but they seem like bad design. Are there any cases where you think ref is the right solution?

好像你总是可以做别的事情就是清洁。有人可以给我在那里这将是一个问题,最好的解决方案的例子吗?

It seems like you could always do something else that is cleaner. Can someone give me an example of where this would be the "best" solution for a problem?

推荐答案

在我看来, REF 宣告新的实用类型的难度和套结上的信息,以现有的信息,这是C#采取了巨大的步骤,朝自成因解决事情的难度在很大程度上补偿通过LINQ,泛型和匿名类型。

In my opinion, ref largely compensated for the difficulty of declaring new utility types and the difficulty of "tacking information on" to existing information, which are things that C# has taken huge steps toward addressing since its genesis through LINQ, generics, and anonymous types.

所以,不,我不认为有很多明确的用例它了。我认为这在很大程度上是怎样的语言最初设计的遗物。

So no, I don't think there are a lot of clear use cases for it anymore. I think it's largely a relic of how the language was originally designed.

我认为它仍然是有意义的(如上面提到的),在你需要返回的情况下某种错误代码从功能以及一个返回值,但没有别的(这样一个更大的类型是不是真的有道理的。)如果我做的这一切在上一个项目的地方,我可能会定义一些通用包装类型的东西加错误代码,但在任何给定的情况下 REF 退出正常。

I do think that it still makes sense (like mentioned above) in the case where you need to return some kind of error code from a function as well as a return value, but nothing else (so a bigger type isn't really justified.) If I were doing this all over the place in a project, I would probably define some generic wrapper type for thing-plus-error-code, but in any given instance ref and out are OK.

这篇关于在C#中,你用与在那里QUOT;参考"在参数的前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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