أسئلة القسم
تدرب على أسئلة المقابلات في هذا القسم. اكتب إجابتك، قم بتقييمها، أو اضغط على "عرض الإجابة" بعد التفكير.
What is the difference between IEnumerable, ICollection, IList, and IDictionary? صعب
- IEnumerable: Forward-only iteration (foreach).
- ICollection: Extends IEnumerable, adds Count, Add, Remove.
- IList: Extends ICollection, supports index-based access.
- IDictionary: Key-value pairs, extends ICollection.
What is the difference between IEnumerable and IQueryable? صعب
Feature | IEnumerable | IQueryable |
---|---|---|
Execution | In-memory (LINQ to Objects) | Deferred, query provider (e.g., EF Core → SQL) |
Filtering | Done in memory | Translated to remote query (DB, API) |
Performance | Can be slower for large data | Efficient for remote data sources |
What is the difference between shallow copy and deep copy in C#? صعب
Shallow Copy:
- Copies object’s fields as is.
- Reference fields still point to the same objects.
- Example: MemberwiseClone().
Deep Copy:
- Copies the object and all referenced objects recursively.
- Requires custom implementation or serialization.
How does the ConcurrentDictionary differ from Dictionary? صعب
Dictionary<TKey,TValue>:
- Not thread-safe for concurrent reads/writes.
- Needs external locking (lock) for multi-threading.
ConcurrentDictionary<TKey,TValue>:
- Designed for thread-safe concurrent access.
- Uses fine-grained locking and lock-free operations internally.
- Provides atomic methods like TryAdd, GetOrAdd, AddOrUpdate.
How does the Span<T> type improve performance in C#? صعب
Span
Benefits:
- Avoids allocations → no new arrays when slicing.
- Improves performance for parsing, processing large data.
- Safe alternative to pointers (bounds-checked).
Limitation: Cannot be stored on heap (because it references stack memory).
Explain covariance and contravariance in generics. صعب
- Covariance (out): lets you use a more derived type than specified. Works with return values.
- Contravariance (in): lets you use a more generic (base) type than specified. Works with parameters.
What is the size of stack in .NET? صعب
By default:
- 1 MB for main thread.
- 256 KB – 1 MB for worker threads (depends on OS and settings).
- Can be configured using Thread constructor or PE header.
Which objects go on Large Object Heap? صعب
- Objects ≥ 85 KB (mainly large arrays, strings, buffers).
- Stored separately for performance (avoid frequent GC compaction).
What are the limitations of garbage collection? صعب
- Non-deterministic (can’t control exactly when GC runs).
- Finalizers delay cleanup.
- Expensive for large heaps (pauses).
- Not aware of unmanaged resources → need IDisposable.
What is the difference between managed and unmanaged environments? (GC pros and cons, JIT pros and cons) صعب
Managed (.NET runtime):
- Pros: GC, memory safety, type safety, security.
- Cons: Overhead (GC pauses, JIT cost).
Unmanaged (C/C++):
- Pros: Full control, high performance.
- Cons: Risk of leaks, buffer overflows, manual memory management.
If an async method runs, does it create a new thread? صعب
- No, async methods don’t create new threads by default.
- I/O-bound async uses I/O completion ports.
- CPU-bound async needs Task.Run() (creates a thread).
What is the difference between Task, Thread, and ValueTask? صعب
- Thread: OS thread, heavy, long-lived.
- Task: abstraction for async work, can use thread pool or I/O.
- ValueTask: lightweight struct for performance when result is often available synchronously.
What are thread-safe collections in .NET? صعب
Collections in System.Collections.Concurrent:
- ConcurrentDictionary, ConcurrentBag, BlockingCollection, ConcurrentQueue, ConcurrentStack.
- Provide lock-free or fine-grained locking.