简体   繁体   English

空对象是什么意思?

[英]What does a null object mean?

If I create an object of a class 如果我创建一个类的对象

A obj = new A();

and later I assign null to it: obj = null; 后来我给你null它: obj = null; .

Then what does it mean obj being null? 那么obj为空意味着什么? Does it mean now it is pointing nowhere and all memory is deallocated? 这是否意味着现在它指向无处,并且所有内存都已释放?

obj now points to the null reference memory location. obj现在指向null参考内存位置。

From null (C# Reference) on MSDN: 从MSDN上的null(C#参考)

The null keyword is a literal that represents a null reference, one that does not refer to any object. null关键字是一种表示空引用的文字,它不引用任何对象。

If you have not assigned the object it formerly pointed to to anything else, that object will be eligible for collection by the GC. 如果尚未分配该对象先前指向的其他任何对象,则该对象将有资格由GC收集。

这意味着它没有指向任何地方,并且内存有资格在将来某个不确定的时间由垃圾回收器重新分配。

null is not an object. null不是对象。 It is a special reference value that references nothing. 这是一个特殊的参考值,不引用任何内容。 If you try to use a null reference to communicate with an object an error is thrown, because there is no object. 如果尝试使用null引用与对象进行通信,则将引发错误,因为没有对象。

There is also a design pattern called the Null Object Pattern , created to solve the problem of the null reference requiring special cases. 还有一种称为“ 空对象模式”的设计模式 ,用于解决需要特殊情况的null引用的问题。 A null object in this pattern is an object that has "neutral" behaviour; 此模式中的空对象是具有“中性”行为的对象。 that is, that when asked to do something actually does nothing, and when asked to return a value returns zeros, empty strings, empty lists, and other safe, neutral objects. 也就是说,当被要求做某事时实际上什么都不做,当被要求返回一个值时,它会返回零,空字符串,空列表以及其他安全的中性对象。

Don't confuse the null object with the special null reference. 不要将null对象与特殊的null引用混淆。

Internally it means that this object in not considered as a root. 在内部,这意味着该对象不被视为根。 and will not be added to the available objects graph. 并且不会添加到可用对象图中。

Hence , it will be considered as collect'able. 因此,它将被视为可收集的。 ( non-deterministic time.). (不确定的时间)。

You need to understand that reference types (classes) are in fact pointers to data in-memory. 您需要了解,引用类型(类)实际上是指向内存中数据的指针。 Essentially, what it does is set the pointer (which is just a piece of memory containing the address of the data) to the value zero, or null. 本质上,它将指针(它只是一块包含数据地址的内存)设置为零或空值。

In practice, this means that if no other references to the data remain, the garbace collector may remove it at any point. 实际上,这意味着如果没有其他对数据的引用,垃圾收集器可以随时将其删除。

Technically, obj is an alternative name for the memory location on heap that contains an object of type A when you say A obj = new A(); 从技术上讲,当您说A obj = new A(); obj是堆上包含一个A类型对象的内存位置的备用名称A obj = new A();

But if you then say obj = null; 但是如果你说obj = null; then obj now references nowhere. 那么obj现在无处引用。

Also the memory that was previously occupied by the object of type A, for which obj was an alias, that will be given back to the operating system by the GC when the GC thinks it is the right time. 同样,以前由类型A的对象占用的内存( obj是其别名)也将由GC认为合适的时候由GC返还给操作系统。

It means obj point to nowhere and original referenced instance of type A goes to gen0. 这意味着obj指向无处,类型A原始引用实例进入gen0。 If A is a type implemented IDisposable , then the resource that A holds are going to be released when Dispose called, and suppressed to be finalized. 如果A是实现为IDisposable的类型,则A所拥有的资源将在Dispose调用时释放,并抑制最终确定。 If a disposable is not explicitly disposed, then it would wait to be finalized. 如果一次性用品没有明确处置,则将等待定稿。

If the execution afterwards reference it again, and it comes gen1 and then possibly gen2, it's life time are going to be longer. 如果执行之后再次引用它,并且它出现在gen1,然后可能是gen2,那么它的生命周期将会更长。

The time when gc comes to collect garbages, the gen0s would be then first choice to be collected, then gen1, gen2. 当gc收集垃圾时,gen0将成为首选,然后是gen1,gen2。

A obj = new A(); obj =新的A();

The reference variable obj is of type A . 参考变量obj的类型为A。 You are creating an instance of A and assigning the reference of it to obj . 您正在创建A的实例,并将其引用分配给obj obj now references the instance of A . obj现在引用A的实例。

obj = null obj =空

The above assignment says the obj now references null . 上面的分配说obj现在引用了null That is nothing. 没什么 Now the newly created instance of A is out of reference and it will be eventually garbage collected . 现在,新创建的A实例已无法使用,最终将被垃圾回收

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM