أسئلة القسم
تدرب على أسئلة المقابلات في هذا القسم. اكتب إجابتك، قم بتقييمها، أو اضغط على "عرض الإجابة" بعد التفكير.
When is the Dispose method called on a class that implements IDisposable? صعب
Dispose() is not called automatically by the CLR.
It must be called explicitly by:
- The developer (
obj.Dispose()
), or - The
using
/using declaration
statement, which ensures automatic disposal.
It is typically called when you are done using an object that holds unmanaged resources (e.g., file handles, DB connections).
What is the difference between Dispose and Finalize? صعب
Dispose | Finalize |
---|---|
Called manually or via using . |
Called automatically by GC. |
Part of IDisposable interface. |
Implemented by overriding ~ClassName() (destructor). |
Deterministic cleanup. | Non-deterministic cleanup (timing depends on GC). |
Faster, avoids GC overhead. | Slower, requires GC to track finalizable objects. |
Are the standard .NET generic collection classes thread-safe by default? صعب
No, collections like List<T>
, Dictionary<TKey,TValue>
are not thread-safe.
For thread-safe alternatives, .NET provides Concurrent Collections (ConcurrentDictionary
, BlockingCollection
, etc.).
Can you explain how method dispatch works internally in C# (vtable)? صعب
CLR uses a vtable (virtual method table) for virtual/abstract/override methods.
Each type has a vtable mapping method slots → function pointers.
When calling a virtual method:
- CLR looks up the object’s type.
- Jumps to the appropriate method pointer in the vtable.
Non-virtual methods = direct calls (no vtable lookup).
What happens under the hood in the CLR when you call a virtual method? صعب
CLR performs a vtable lookup at runtime.
The exact implementation (base or derived) is resolved based on the object’s runtime type, not compile-time type.
This enables runtime polymorphism.
What is object slicing, and does it apply in C#? صعب
Object slicing: In C++ when assigning a derived object to a base object by value, extra fields are "sliced off".
In C#: This doesn’t apply because:
- Objects are reference types by default.
- Only value types (structs) could cause similar behavior, but C# disallows slicing scenarios directly.
How does the CLR handle multiple interface inheritance when the interfaces have the same method signatures? صعب
CLR creates separate interface method tables.
If two interfaces declare the same method signature:
- A single implementation can satisfy both.
- Or explicit interface implementation can disambiguate.
What’s the difference between IDisposable and IAsyncDisposable? صعب
IDisposable | IAsyncDisposable |
---|---|
Synchronous cleanup (Dispose() ). |
Asynchronous cleanup (ValueTask DisposeAsync() ), useful for async I/O (e.g., Stream.DisposeAsync() ). |
How does memory management in .NET (GC + IDisposable) affect object lifetime? صعب
GC: Manages memory for managed objects (allocates on heap, reclaims unreachable ones).
IDisposable: Allows deterministic cleanup of unmanaged resources before GC collects the object.
Together: GC frees managed memory, Dispose()
handles unmanaged resources (files, sockets, DB connections).
What is the diamond problem in OOP, and how does C# solve it? صعب
Diamond problem: In multiple inheritance, ambiguity arises when two base classes define the same method.
C# solution:
- No multiple class inheritance.
- Multiple interface inheritance is allowed, and ambiguity is solved via explicit interface implementation.