inout 参数有什么用处吗?
inout(C# 中的 ref,vb.net 中的 byref(如 out 参数))参数在 .NET 中是否有很好的用途?
我觉得一个参数同时用作输入和返回值所造成的混乱比增加输出参数的参数数量,或者返回数组或者返回自定义类更糟糕。
IS there a good use for inout (ref in C#, byref (like out parameters) in vb.net) parameters in .NET?
I feel that the confusion caused by a parameter used both as input and as a return value is worse that the increase in the number of parameters for out parameters, or returning an array or returning an custom class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我遇到的最常见的用途(在我看来,这仍然不常见)是一种“修改现有对象,或者在必要时创建一个对象”。 例如:
StringBuilder
可能不是一个很好的例子,但这意味着您有时可以在不需要时避免创建对象:The most common use (which still isn't that common, IMO) I've come across is a sort of "modify an existing object, or create one if necessary". For example:
StringBuilder
probably isn't a great example, but it means you can sometimes avoid creating an object when you don't need to:我主要使用它来适应遗留代码。=(com 互操作)。
我还倾向于在需要高性能并指示成功的代码中使用它:
其中返回值是成功或失败的概念,或者可能是错误代码,defaultedOutput 是带有默认值的值。
您是否知道有 out 和 ref 之间没有真正的区别(至少就 CLR 而言)?
I've used it mostly for adapting to legacy code.= (com interop).
I also tend to use it in code that needs to be performant and indicate success:
where the return value is a notion of success or failure or perhaps an error code and defaultedOutput is a value that comes in with a default value.
Did you know that there is no real difference between out and ref (at least as far as the CLR is concerned)?
在某些极少数情况下,它可能很有用,主要是出于性能原因。
在大多数情况下,它可以,而且在我看来,应该通过返回您提到的数组或自定义类来避免。
In some rare cases it can be useful, mostly for performance reasons.
In most cases it can, and in my opinion it should, be avoided by returning an array or a custom class as you mention.
当您可能进行一系列修改同一变量的调用时。
不过,在像 C# 这样的基于指针的语言中,这种情况不会发生太多,因为您只需将对象作为“in”参数传递,并且被调用的函数可以调用其方法来根据需要修改它。
When you are likely to make a series of calls that modify the same variable.
It doesn't happen much in pointer-based languages like C# though, because you can just pass an object as an 'in' parameter and the called function can call its methods to modify it as necessary.
Ref 和 out 参数传递模式用于允许方法更改方法调用者传入的变量。
每种参数传递模式(ref 和 out)都是为了满足不同的编程需求而设计的。
采用输出参数的方法的调用者不需要在调用之前分配给作为输出参数传递的变量; 但是,该方法需要在返回之前分配给 out 参数。
考虑out参数的一种方法是,它们就像方法的附加返回值。 当方法应该返回多个值时,它们很方便。
不要将引用传递的概念与引用类型的概念混淆。
这两个概念没有关联; 方法参数无论是值类型还是引用类型都可以通过 ref 进行修改,通过引用传递时不存在值类型的装箱。
Ref and out parameter passing modes are used to allow a method to alter variables passed in by the method caller.
Each parameter passing mode (ref and out) is designed to suite different programming needs.
The caller of a method which takes an out parameter is not required to assign to the variable passed as the out parameter prior to the call; however, the method is required to assign to the out parameter before returning.
One way to think of out parameters is that they are like additional return values of a method. They are convenient when a method should return more than one value.
Do not confuse the concept of passing by reference with the concept of reference types.
The two concepts are not related; 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.
我在图形例程中将它与值类型参数一起使用,以垂直布局将文本打印到 GDI 窗口。 inout 参数跟踪当前的 Y 位置:
而不是
I've used it with an value-type parameter in a graphics routine that printed text to a GDI window in a vertical layout. The inout parameter kept track of the current Y position:
rather than
Out 适用于需要多个返回值的简单情况。 这样您就不会因
ref
可能导致的“它是一个参数/它是一个返回值”而感到困惑。Out is good for simple cases which require multiple return values. This way you don't get confused by "is it a parameter/is it a return value" that
ref
might cause.当我想要修改一个值并且认为包装类太过分时,我会使用它。
例如,
在此示例中,Parse 函数可能会消耗不止一行。 因此他们负责正确更新索引变量。
I use it when I want to modify a value and think a wrapper class is overkill.
For example
In this example the Parse functions may consume more than one line. Thus they are responsible for correctly updating the index variable.
你确实回答了你自己的问题。
如果通过参数传入和传出数据有意义 - 即。 如果该方法需要知道当前值并且还希望更新它(或者,在引用类型的情况下,替换它),那么 ref 是正确的。 这种情况并不经常发生,但当它发生时,你知道该使用什么;-)
You've really answered you own question.
If it makes sense to pass data both in and out through a parameter - ie. if the method needs to know the present value and also is expected to update it (or, in the case of a reference type, replace it), then ref is right. It doesn't happen often, but when it does, you know what to use ;-)