C#外部参数

发布于 2024-12-05 06:58:03 字数 412 浏览 0 评论 0原文

下面两个代码片段有什么区别?

public void foo(out classA x)
{
    y = new classA();
    x = y;
}

第二个:

public void foo(out classA x)
{
    classA y;
    x = y;
}

第二个片段是否危险,因为 x 现在拥有对此本地 y 的引用,该引用在退出 foo 后可能已经死亡?

为什么我们一般都要用“新”?

我有点困惑,因为在 C++ 中,如果第二个片段中的 x 是一个指针,则语句 x = y 甚至不会编译,因为 y 不是指针。

What is the difference between the following two code snippets?

public void foo(out classA x)
{
    y = new classA();
    x = y;
}

and the second:

public void foo(out classA x)
{
    classA y;
    x = y;
}

Is it true that the second snippet is dangerous, because x has now a reference to this local y, which might be already dead after exiting foo?

Why do we have to use "new" in general?

I'm a bit confused, because in C++, if x in the second snippet would be a pointer, the statement x = y would not even compile since y is not a pointer.

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

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

发布评论

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

评论(4

泡沫很甜 2024-12-12 06:58:03

让我们暂时假设第二个片段是

public void foo(out classA x)
{
  classA y = new classA();
  x = y;
}

片段,因为您写的它根本不会在C#中编译。 y必须先分配。行clasta y;不会像在C ++中那样在堆栈上创建clasta的实例。它只是声明类型clasta的变量y

考虑到编译片段,将out变量分配给本地声明的变量,并且初始化的对象在任何方面都不是危险的。 classa y指向并分配给x的对象将保持活力,直到x熄灭范围的范围在何处/使用/使用。

Let's assume for a moment that the second snippet is

public void foo(out classA x)
{
  classA y = new classA();
  x = y;
}

The snippet as you wrote it does not compile in C# at all. y has to be assigned first. The line classA y; does not create an instance of classA on the stack as it would in C++. It simply declares a variable y of type classA.

With the compiling snippet in mind, assigning an out variable to a locally declared and initialized object is not dangerous in any way. The classA object that y points to and that is assigned to x will remain alive and well until x goes out of scope wherever it's declared/used.

你曾走过我的故事 2024-12-12 06:58:03

我认为您的困惑在于,在 C++ 中,第二个示例将返回对堆栈分配对象的引用。在 C# 中不会发生这种情况。

给出 C# 中的第二个示例:

public void foo(out classA x)
{
  classA y;  // Doesn't allocate an object
  x = y;
}

无论如何,这可能无法编译,因为 y 从未分配过值。

Your confusion, I think, is that in C++ the second example would return a reference to a stack-allocated object. That doesn't happen in C#.

Given your second example in C#:

public void foo(out classA x)
{
  classA y;  // Doesn't allocate an object
  x = y;
}

This probably won't compile, anyway, because y is never assigned a value.

老娘不死你永远是小三 2024-12-12 06:58:03

第二个片段真的很危险吗[...]

不在 C# 中;您正在考虑像 C 或 C++ 这样的本地语言,它们允许使用指向局部变量的指针。在 C# 中,一切(在合理范围内)都是对托管对象的引用。一切本质上都在堆上。

Is it true that the second snippet is dangerous [...]

Not in C#; you're thinking of native languages like C or C++ that allow one to take a pointer to a local variable. In C#, everything (within reason) is a reference to a managed object. Everything is essentially on the heap.

怕倦 2024-12-12 06:58:03

这并不危险,因为除非在方法返回或退出之前给出所有“out”参数的定义,否则代码将无法编译。

It is not dangerous because the code will NOT be able to be compiled unless all "out" parameters are given a definition before the method returns or exits.

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