C# Technical Interview Flashcards

1
Q

What is the difference between IEnumerable and IQueryable interfaces in C#? When would you use each one?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the differences between async/await and Task.Run in C# when dealing with asynchronous code?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the various methods of sharing data between tasks in C#.

A

Data can be shared between tasks using thread-safe data structures like

ConcurrentDictionary and ConcurrentQueue or synchronization constructs like lock, Monitor, and Semaphore.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you implement a custom exception handling middleware in ASP.NET Core?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you implement a custom attribute in C#? Provide an example of how to use it to decorate a class.

A

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

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the differences between covariance and contravariance in C# for delegates and interfaces.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you handle deadlocks in multi-threaded C# applications?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the using directive and the using statement in C#. What are their differences?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the differences between value types and reference types in C#.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you implement a custom IComparer<T> for sorting in C#?</T>

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain the concept of object pooling in C# and its benefits in multi-threaded applications.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you improve string concatenation performance in C#?

A

Use StringBuilder instead of repeated string concatenation with + to improve performance, especially when concatenating or linking multiple strings together in a loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are delegates and events in C#? Provide an example of using events in a C# application.

A

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);

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the different types of garbage collection in C#? How can you configure them?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the differences between a deep copy and a shallow copy of objects in C#. How can you perform each type of copy?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is reflection in C#? How is it useful, and what are its limitations?

A

Reflection allows inspecting and manipulating types, methods, properties, etc., at runtime. It’s useful for building generic frameworks and creating dynamic and extensible applications. However, it can be slower and lacks compile-time safety.

17
Q

What is the purpose of the yield keyword in C#? Provide an example of using it with an iterator.

A

The yield keyword is used to create an iterator in C#. It allows you to return a sequence of values without building the entire sequence in memory before returning it.

For example:

public IEnumerable<int> GetNumbers()</int>

{

for (int i = 0; i < 10; i++)

{

    yield return i;

}

}

18
Q

Explain how to implement a custom serializer and deserializer for a complex object in C#.

A

You can implement custom serialization logic by manually converting the object’s properties to a serialized format (e.g., JSON or XML). Then, implement the deserialization logic to read the serialized data and recreate the object.

19
Q

Explain the differences between Memory<T> and Span<T> in C#. When would you use one over the other?</T></T>

A

Memory<T> represents a chunk of memory that can be read from and written to. Span<T> is a lightweight view of a portion of an array or memory.</T></T>

Use Memory<T> when you need a dynamically allocated memory block and Span<T> to work with a portion of an existing array or memory.</T></T>

20
Q

How can you optimize memory usage and performance when working with large data collections in C#?

A

You can consider using data structures that are more memory-efficient for specific scenarios, like BitArray for managing sets of bits or HashSet<T> for effective membership checks. Also, you can use streaming and lazy-loading techniques to avoid loading the entire data set into memory.</T>

21
Q

What are extension methods in C#? Provide an example of using an extension method with an existing class.

A

Extension methods are created by defining static methods within static classes. The “this” keyword is used in the method’s parameter to indicate the type being extended.

For example:

public static class StringExtensions{ public static bool IsPalindrome(this string str)

{

    // Check if the string is a palindrome

    // ...

}

}

// Usage:

string word = “racecar”;

bool isPalindrome = word.IsPalindrome(); // true

22
Q

How do you work with parallelism and concurrency in C#? Explain the differences between Parallel.ForEach and PLINQ (Parallel LINQ).

A

Parallel.ForEach is used to parallelize the iteration over a collection, while PLINQ allows performing parallel queries on data using LINQ. Use Parallel.ForEach when you need to apply a function to each element of a collection in parallel. Use PLINQ when you want to perform data queries in parallel.

23
Q

How does the volatile keyword work in C#? When and how should you use it?

A

The volatile keyword is used to ensure that the value of a variable is always read from and written to the main memory, not from a cache. Use it when you have a variable shared between multiple threads and want to avoid potential visibility issues or stale values.

24
Q

Explain the differences between async void and async Task in C#. When would you use one over the other?

A

async void is used for event handlers and should be avoided in most other scenarios since it makes error handling more difficult. async Task is used for asynchronous methods that return a result and allow better error handling through Tasks’ Exception property.

25
Q

What is boxing and unboxing in C#? How can you avoid performance issues related to boxing?

A

Boxing is the process of converting a value type to an object type, and unboxing is the reverse process. Boxing and unboxing can cause performance overhead. To avoid it, use generics (List<T>, Dictionary<TKey, TValue>, etc.) over non-generic collections (ArrayList, Hashtable, etc.), and use generic versions of interfaces like IEnumerable<T> instead of non-generic versions like IEnumerable.</T></T>

26
Q

What is a Singleton design pattern?

A

The singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It typically involves a private constructor and a static method to retrieve the instance.

27
Q

Explain the Factory Method design pattern.

A

The factory Method pattern defines an interface for creating objects but allows the sub-class to decide which class to instantiate. It promotes loose coupling between the client code and the objects.