创建新对象而不分配给引用 [英] creating new objects without assignment to a reference

查看:96
本文介绍了创建新对象而不分配给引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想知道创建新对象而无需分配给

参考像这样:


...

新员工(John,Woo);

.. ..


这是一个很好的编程习惯吗?垃圾收集器

如何处理这个问题?

Hello,

I want to know about "creating new objects without assignment to a
reference" like this :

...
new Employee("John","Woo");
....

Is this a good programming practice ? How does garbage collector
handle this issue ?

推荐答案

Hi Murat,


当代码离开当前代码块并被GC吃掉时,对象将超出范围。您需要一些存储引用的方法,因为这是GC确定是否应该销毁对象的方式。没有参考 - >吃吧!


2005年5月3日星期二09:29:25 +0200,Murat Ozgur< mu ******** @ gmail.com>写道:
Hi Murat,

The object will fall out of scope when your code leaves the current code block and eaten by GC. You need some way of storing a reference as this is how GC determines if an object should be destroyed. No reference -> Eat it!

On Tue, 03 May 2005 09:29:25 +0200, Murat Ozgur <mu********@gmail.com> wrote:
你好,

我想知道创建新对象而不分配
参考像这样:

...
新员工(John,Woo);
....

这是一个好的编程习惯?垃圾收集器如何处理这个问题?
Hello,

I want to know about "creating new objects without assignment to a
reference" like this :

...
new Employee("John","Woo");
....

Is this a good programming practice ? How does garbage collector
handle this issue ?




-

快乐编码!

Morten Wennevik [C#MVP]



--
Happy coding!
Morten Wennevik [C# MVP]


该对象是立即垃圾,除非它被用作参数。如果是参数...


static void Foo(对象o)

{

}


static void Main()

{

Foo(new object());

}


然后在这种情况下,对象以参数o为根,当方法完成时(可能在依赖Foo实现之前),对象将是垃圾 - 除非它将对象分配给某个地方的某个字段延长其生命周期。


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


你好,


我想知道创建新对象而不分配给

参考像这样:


...

新员工(John,Woo);

.. ..


这是一个很好的编程习惯吗?垃圾收集器

如何处理这个问题?

That object is immediate garbage unless it is being used as a parameter. If it is a parameter ...

static void Foo(object o)
{
}

static void Main()
{
Foo( new object() );
}

then in this case the object is rooted by the parameter o and when the method finishes (possibly before depending on the Foo implementation) the object will be garbage - unless it assigns the object to a field somewhere to extend its lifetime.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hello,

I want to know about "creating new objects without assignment to a
reference" like this :

...
new Employee("John","Woo");
....

Is this a good programming practice ? How does garbage collector
handle this issue ?


对象永远不在范围内 - 它没有分配给任何东西 - 它的瞬间垃圾一旦GC运行就会收集它 - 无论是在代码块还是以后


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi Murat,


当代码离开当前代码块并被GC吃掉时,该对象将超出范围。您需要一些存储引用的方法,因为这是GC确定是否应该销毁对象的方式。没有参考 - >吃吧!
The object is never in scope - its not assigned to anything - its instant garbage as soon as the GC runs it will collect it - whether its in the code block or later

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi Murat,

The object will fall out of scope when your code leaves the current code block and eaten by GC. You need some way of storing a reference as this is how GC determines if an object should be destroyed. No reference -> Eat it!


这篇关于创建新对象而不分配给引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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