c# - 使用 out 和 ref 参数时的装箱和拆箱

标签 c# boxing ref out unboxing

当方法接受 ValueType 的 out/ref 参数时是否会发生装箱/拆箱?

最佳答案

对于 ref 关键字它已经在 MSDN 上提到了那:

Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. 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.

关于out关键字:

The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed.

关于c# - 使用 out 和 ref 参数时的装箱和拆箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5103147/

相关文章:

rust - 装箱类型时是否有最佳实践?

c# - Windows Phone 中的 XNA RenderTarget2D

c# - 为什么这段代码的执行速度比预期的要快?

c# - 这个 ASP.NET MVC ActionMethod 应该有两个 View 吗?

java - 在 Java 中实现基本类型类型安全的最高效方法?

c# - 这些装箱/拆箱示例

javascript - Vuejs 动态添加 ref undefined

c# - 为什么迭代器方法不能采用 'ref' 或 'out' 参数?

c# - 如何解决 C# 中的错误 "ByRef return value not supported in reflection invocation"?

c# - 在 C# 中将 DataGridView DataSource 设置为 DataTable 很慢