在C++中,可以删除this并设置自己对null的引用.
我想将一个对象实例设置为null.
public class Foo
{
public void M( )
{
// this = null; // How do I do this kind of thing?
}
}
Run Code Online (Sandbox Code Playgroud)
这在.NET中是不可能的.您无法在对象本身内将当前实例设置为null.在.NET中,当前的instance(this)是readonly,你不能为它赋值.顺便说一句,这在.NET中甚至不需要.
this实际上,它只是arg0在实例方法中赋予参数的特殊名称.将其设置为null是不允许的:
this一个方法上的实例方法classthisa上的实例方法struct,但不能分配null1.的原因是它不会有用:
arg0是by-val(而不是-ref)class,因此方法的调用者不会注意到更改(为了完整性:arg0是struct实例方法的by-ref )null不会删除它; GC处理它所以基本上,不允许使用该语法,因为它不能满足您的需求.有没有你想要的东西C#比喻.