Câu hỏi phỏng vấn C#
Câu hỏi

Explain what is We...

Câu trả lời

Weak Reference in C#

A weak reference in C# is a special type of reference that allows an object to be garbage collected even if the weak reference is still active. Unlike strong references, which prevent an object from being garbage collected, weak references do not contribute to an object's reference count.

Here are the key points about weak references in C#:

Purpose

  • Weak references are used to implement caching mechanisms, where objects are kept in memory only if they are actively being used.
  • They are also used in scenarios where you want to track objects without preventing them from being garbage collected.

Behavior

  • When an object is only accessible through weak references, it becomes eligible for garbage collection.
  • The garbage collector can collect the object even if there are active weak references to it.
  • If an object is collected, the weak reference becomes null.

Implementation

  • In C#, you can create weak references using the WeakReference or WeakReference<T> class.
  • WeakReference is used for objects of any type, while WeakReference<T> is used for strongly typed objects.
  • You can check if the referenced object is still alive using the IsAlive property of the WeakReference...
expert

expert

Gợi ý câu hỏi phỏng vấn

middle

Is there a way to catch multiple exceptions at once and without code duplication?

middle

What is the difference between Virtual method and Abstract method?

senior

What is the yield keyword used for in C#?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào