【发布时间】:2014-06-27 23:16:29
【问题描述】:
static void Main(string[] args)
{
foo f1 = new foo();
string s1 = f1.fooMethod();
string s2 = (new foo()).fooMethod();
// Does anonymous object destroys here?
// some more code....
//....
//....
//....
//....
// f1 is accessible here also
// some more code....
//....
//....
//....
//....
// f1 is accessible here also
}
class foo
{
public string fooMethod()
{
return "fooMethod called";
}
}
在上面的代码中,我使用(new foo()) 语法创建了一个匿名对象(我想这就是这些对象的名称)和一个f1 对象。
现在f1 可以在整个代码块中访问,但那个匿名对象不是(当然)。
问题是:
- 该匿名对象是否会在下一行销毁 工作完成了吗?
- 这种创建匿名对象的方法好还是
与
f1对象创建相比不好(特别是当我们 在这种情况下只需要调用该类的一个方法)?
【问题讨论】:
标签: c# .net memory-management garbage-collection clr