C# Technical Interview Flashcards
What is the difference between IEnumerable and IQueryable interfaces in C#? When would you use each one?
IEnumerable is used for querying in-memory collections, while IQueryable is used for querying external data sources, like databases. IQueryable allows you to write and execute more complex queries on the server.
What are the differences between async/await and Task.Run in C# when dealing with asynchronous code?
Async/await is used to create asynchronous methods that can be awaited without blocking the main thread. Task.Run is used to execute a delegate or lambda expression on a ThreadPool thread asynchronously. It can be helpful to offload CPU-bound work from the main thread.
Explain the various methods of sharing data between tasks in C#.
Data can be shared between tasks using thread-safe data structures like
ConcurrentDictionary and ConcurrentQueue or synchronization constructs like lock, Monitor, and Semaphore.
How do you implement a custom exception handling middleware in ASP.NET Core?
Here’s a summary of the process:
Implement a custom middleware that handles exceptions.
Use app.UseMiddleware to add the middleware to the application’s request pipeline.
In the middleware, catch exceptions, log them, and return an appropriate response.
How do you implement a custom attribute in C#? Provide an example of how to use it to decorate a class.
Implement a custom attribute by creating a class that derives from Attribute. Add properties or fields to store data associated with the attribute. To use the attribute, apply it to classes or members using square bracket syntax.
Here’s an example:
[AttributeUsage(AttributeTargets.Class)]
public class CustomAttribute: Attribute
{
public string Name { get; set; } public CustomAttribute(string name) { Name = name; }
}
[Custom(“ExampleClass”)]
public class MyClass
{
// Class Implementation
}
Explain the differences between covariance and contravariance in C# for delegates and interfaces.
Covariance allows using a more derived type instead of a less derived type, while contravariance enables the opposite. In C#, covariance can be applied to interfaces using the out keyword, and contravariance can be applied using the in keyword.
How do you handle deadlocks in multi-threaded C# applications?
Deadlocks occur when multiple threads are blocked, waiting for each other to release resources. To handle deadlocks, follow best practices like acquiring locks in a consistent order, using timeout mechanisms, and avoiding long-running locks.
Explain the using directive and the using statement in C#. What are their differences?
The using directive is used to include namespaces in a file, making types in those namespaces accessible without fully qualifying their names. The using statement is used for the automatic disposal of resources that implement IDisposable.
Explain the differences between value types and reference types in C#.
Value types store their value directly in memory, while reference types store a reference to an object in memory. Value types are stored on the stack, and reference types are stored on the heap.
How do you implement a custom IComparer<T> for sorting in C#?</T>
Create a class that implements the IComparer<T> interface, defining the Compare method for comparing two objects. Then, use an instance of this custom comparer with the Sort method of a collection to apply your customized sorting logic.</T>
Explain the concept of object pooling in C# and its benefits in multi-threaded applications.
Object pooling involves reusing objects instead of creating new ones. This can improve performance and reduce garbage collection overhead, especially in multi-threaded applications, where object creation and destruction can be costly.
How can you improve string concatenation performance in C#?
Use StringBuilder instead of repeated string concatenation with + to improve performance, especially when concatenating or linking multiple strings together in a loop.
What are delegates and events in C#? Provide an example of using events in a C# application.
Delegates are function pointers that enable encapsulating a method and calling it indirectly. Events are a way to provide notifications to other parts of the application when something happens.
Here’s an example:
public delegate void EventHandler(object sender, EventArgs e);
public class EventPublisher
{
public event EventHandler SomethingHappened; public void DoSomething() { // ... do something OnSomethingHappened(); } protected virtual void OnSomethingHappened() { SomethingHappened?.Invoke(this, EventArgs.Empty); }
}
What are the different types of garbage collection in C#? How can you configure them?
C# supports three types of garbage collection: Workstation garbage collection, Server garbage collection, and Concurrent garbage collection. You can configure garbage collection behavior using configuration settings in the app’s configuration file.
Explain the differences between a deep copy and a shallow copy of objects in C#. How can you perform each type of copy?
A shallow copy creates a new object but doesn’t duplicate internal references. A deep copy creates a new object and clones all internal references recursively. Shallow copying can be done using MemberwiseClone, while deep copying requires custom implementation.