C# Out 参数问题:Out 如何处理值类型?

发布于 2024-07-11 20:25:35 字数 1144 浏览 10 评论 0原文

更新所以完全拉动了一个工具时刻。 我真正的意思是参考与输出/参考。 任何说“ref”的东西我真正的意思是引用,如

SomeMethod(Object someObject)

SomeMethod(out someObject)

抱歉。 只是不想更改代码,因此答案已经有意义。

据我了解,与 ref 不同的是,它“复制”指针并在堆栈上创建一个新空间来使用该指针,但不会' t 改变指针:

SomeMethod()
{
 SomeThing outer = new SomeThing();
 RefMethod(ref outer)
}

RefMethod(ref inner)  //new space on stack created and uses same pointer as outer
{
   inner.Hi = "There"; //updated the object being pointed to by outer
   inner = new SomeThing();//Given a new pointer, no longer shares pointer with outer
                           //New object on the heap
}

Out 复制指针并可以操作它指向的位置:

SomeMethod()
{
 SomeThing outer = new SomeThing();
 RefMethod(out outer)
}

RefMethod(out inner)  //same pointer shared
{

   inner = new SomeThing();//pointer now points to new place on heap  
                           //outer now points to new object
                           //Old object is orphaned if nothing else points to it
}

这对于对象来说很好,但是对于值类型来说呢,因为它们没有任何东西可以指向,只存在于堆栈上?

UPDATE So totally pulled a tool moment. I really meant by reference versus Out/Ref. Anything that says 'ref' I really meant by reference as in

SomeMethod(Object someObject)

Versus

SomeMethod(out someObject)

Sorry. Just don't want to change the code so the answers already make sense.

Far as I understand, unlike ref where it "copies" the pointer and creates a new space on the stack to use that pointer, but won't change the pointer:

SomeMethod()
{
 SomeThing outer = new SomeThing();
 RefMethod(ref outer)
}

RefMethod(ref inner)  //new space on stack created and uses same pointer as outer
{
   inner.Hi = "There"; //updated the object being pointed to by outer
   inner = new SomeThing();//Given a new pointer, no longer shares pointer with outer
                           //New object on the heap
}

Out copies the pointer and can manipulate where it points to:

SomeMethod()
{
 SomeThing outer = new SomeThing();
 RefMethod(out outer)
}

RefMethod(out inner)  //same pointer shared
{

   inner = new SomeThing();//pointer now points to new place on heap  
                           //outer now points to new object
                           //Old object is orphaned if nothing else points to it
}

That's fine and dandy with objects, but what about value types seeing as they have nothing to point to being only on the stack?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻找一个思念的角度 2024-07-18 20:25:35

仅仅因为变量存在于堆栈上(如果它是局部变量)并不意味着您不能创建指向它的指针 - 事实上引用类型也是如此。

RefMethod 中的指针指向“外部”变量 - 并且该变量本身位于堆栈中,因为它是未捕获的局部变量。

正如 Leppie 所说,除了明确赋值的规则之外,ref 和 out 是相同的 - 事实上,IL 中的唯一区别是应用于 out 参数的属性。

有关 ref/out 的更多详细信息,请参阅我关于参数传递的文章

Just because the variable lives on the stack (if it's a local variable) doesn't mean you can't create a pointer to it - and indeed that's the case with reference types as well.

The pointer within RefMethod is to the "outer" variable - and the variable itself lives on the stack as it's an uncaptured local variable.

As Leppie said, ref and out are identical except for the rules on definite assignment - in fact, the only difference in IL is an attribute applied to out parameters.

See my article on parameter passing for more details about ref/out in general.

恍梦境° 2024-07-18 20:25:35

据我所知,ref 和 out 是完全相同的,只是 out 参数无法初始化。 因此两者都会入栈。

ref and out is exactly the same, as far I know, with the exception that an out parameter cannot be initialized. Hence both goes on the stack.

假装不在乎 2024-07-18 20:25:35

实际上,在引用类型上使用 ref 或 out 也会创建一个指针......不是指向对象,而是指向对象的引用!
所以在 C++ 中它是某种类型

RefMethod(SomeThing **inner)
{
}

,而对于值类型则它是

RefMethod2(int *inner)
{
}

值类型。

Actually using ref or out on Reference types also creates a pointer...not to the object but to the reference to the object!
So it would be some kind of

RefMethod(SomeThing **inner)
{
}

in C++, while with value types it would be

RefMethod2(int *inner)
{
}

for value types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文