أسئلة القسم
تدرب على أسئلة المقابلات في هذا القسم. اكتب إجابتك، قم بتقييمها، أو اضغط على "عرض الإجابة" بعد التفكير.
Can you explain partial classes and their limitations? متوسط
Partial class: Lets you split a class across multiple files using the partial
keyword.
Commonly used in auto-generated code (e.g., Entity Framework, WinForms).
Limitations:
- All parts must have
partial
. - Must be in the same assembly and namespace.
- Cannot span multiple assemblies.
What is an extension method in C#? When would you use one? متوسط
Extension method: A static method that appears as if it’s part of an existing type.
Declared in a static class with this
keyword on the first parameter.
Use case: Add methods to existing classes (e.g., LINQ adds extension methods to IEnumerable<T>
).
What is a virtual method, and why would you use it? متوسط
Virtual method: A method in a base class marked with virtual
.
- Can be overridden in a derived class using
override
. - Provides runtime polymorphism.
Can a struct have virtual methods in C#? متوسط
No.
Structs are value types and do not support inheritance, so virtual/abstract methods are not allowed.
However, structs can implement interfaces.
What’s the difference between using the new keyword and the override keyword? متوسط
- override: Used when the base method is marked
virtual
,abstract
, oroverride
. It replaces the base implementation. - new: Hides the base class method. The base method still exists but is hidden in the derived class.
Key difference: override
= polymorphism (runtime binding). new
= method hiding (compile-time).
Can a class inherit from multiple classes in C#? Why or why not? متوسط
No, C# does not allow multiple inheritance of classes.
Reason: To avoid diamond problem (ambiguity when two parents have the same method/property).
Instead: C# supports multiple interfaces.
Is it possible for a class to implement multiple interfaces? متوسط
Yes, a class can implement multiple interfaces.
This is how C# provides multiple inheritance of behavior safely.
What does the out keyword specifically mean? متوسط
The out
keyword means the called method is responsible for assigning a value before returning.
Commonly used for methods returning multiple values.
How does an object initializer work in C#? متوسط
Object initializer: Provides a clean way to initialize properties when creating an object.
Reduces need for multiple constructors.
What is the difference between early binding and late binding? متوسط
- Early Binding (compile-time): Type and method resolution happens at compile-time (e.g., method calls on normal objects).
- Late Binding (runtime): Resolution happens at runtime (e.g., reflection,
dynamic
).
What are generics in C#? Can you explain with an example? متوسط
Generics allow type-safe, reusable code without sacrificing performance.
Example: List<T>
, Dictionary<TKey, TValue>
.
Can you explain covariance and contravariance in .NET generics? متوسط
- Covariance (out): Allows a method to return a more derived type (e.g.,
IEnumerable<Derived> → IEnumerable<Base>
). - Contravariance (in): Allows a method to accept less derived type (e.g.,
Action<Base> → Action<Derived>
). - Invariance: No conversion allowed.
What is an indexer in C#? متوسط
Indexer: Special property that lets a class/struct be accessed like an array.