Q1. What is IQueryable and how is it different from IEnumerable?

Execution: IQueryable has deferred execution, while IEnumerable has immediate execution. With IQueryable, the query is not executed until the results are actually needed, such as when calling ToList() or FirstOrDefault(). With IEnumerable, the query is executed immediately when you call a method like ToList() or FirstOrDefault(), without the possibility of delaying the execution.

Filtering: IQueryable supports server-side filtering, while IEnumerable supports client-side filtering. With IQueryable, you can filter data on the server side by building a query that is executed against a database or other data source, resulting in faster and more efficient querying. With IEnumerable, filtering is done on the client side, which can be slower and less efficient, especially with large datasets.

Performance: IQueryable is generally more performant than IEnumerable for large datasets. Because IQueryable supports server-side filtering and deferred execution, it can be more efficient at processing large datasets by reducing the number of records that need to be loaded into memory. In contrast, IEnumerablewill load all records into memory before processing them, which can result in poor performance and high memory usage with large datasets.

Q2.- What is the difference between FirstOrDefault and SingleOrDefault in C#?

  Data Return- FirstOrDefault() returns the first element that matches the predicate, while SingleOrDefault() throws an exception if more than one element matches the predicate. If there are no matches, both methods return the default value for the type.

Q3. Why is it recommended to use Task as return type over void in the async method?

  The main difference between async void and async Task is that async
void is used for asynchronous methods that don’t return a value and can’t be
awaited, while async Task is used for asynchronous methods that return a Task object and can be awaited.

It’s generally recommended to use async Task instead of async void, unless you’re writing an event handler where the method needs to match a delegate signature that returns void.eturns the first element that matches the predicate, while SingleOrDefault() throws an exception if more than one element matches the predicate. If there are no matches, both methods return the default value for the type.

Q4. Can you explain the difference between ConfigureAwait(false) and ConfigureAwait(true)?

 ConfigureAwait(false) and ConfigureAwait(true) is that ConfigureAwait(false) schedules the continuation of the execution of the method after the await statement to a different context (e.g., thread pool), 

while ConfigureAwait(true) schedules the continuation to the same context as the calling method (e.g., UI thread or thread pool thread). 

It’s generally recommended to use ConfigureAwait(false) in library code that doesn’t depend on the context, and use ConfigureAwait(true) in code that depends on the context, such as UI code

Q6. What is the difference between Task.Run and Task.Factory.StartNew?

both Task.Run and Task.Factory.StartNew are used to start a new Task for concurrent execution. However, there are some differences between them in terms of default behavior and recommended usage.

  1. Default Scheduler:
    • Task.Run uses TaskScheduler.Default, which is typically the ThreadPool.
    • Task.Factory.StartNew without specifying a TaskScheduler uses TaskScheduler.Current if there is one, otherwise, it also uses TaskScheduler.Default.
  2. Exception Handling:
    • Task.Run has better exception handling. It captures and marshals exceptions on the calling context.
    • Task.Factory.StartNew requires more careful handling of exceptions, and you need to ensure that exceptions are observed.
  3. Continuation Options:
    • Task.Run automatically sets TaskCreationOptions.DenyChildAttach to avoid the task being an attachment point for child tasks.
    • Task.Factory.StartNew does not set TaskCreationOptions.DenyChildAttach by default.

Q7. What is the difference between multi-threaded , Parallel and Asynchronous programming?

Multi-threading is a programming paradigm where multiple threads within a process execute independently and concurrently(simultaneously). It is important to understand the multi-threading may or may not be parallel.

Multi-threading can be parallel or interleaved (different thread takes turn to execute) depending upon the number of CPUs or Cores and how OS schedules the thread . Parallel” execution means that multiple threads are truly executing simultaneously, each on its own processor core or hardware thread. In case If there are more threads than available processor cores, or if the operating system’s scheduler decides to switch between threads for other reasons, the threads may be interleaved i.e. the threads would take turn running on a single processor core. Usually the switching happens so fast that it appears that they are running parallely.

Asynchronous programming allows you to work on multiple tasks concurrently without blocking the execution of the calling thread. Does it sounds like Multi-thread of Parallel programming? It is important to understand that it is different from them as it doesn’t require you to run the other task on the separate thread to run concurrently. For e.g. When you do I/O-bound operations like file-system accesses, HTTP requests, API calls, or database queries the thread hands over the processing to the other hardware and just wait for the execution to complete in case of synchronous call. If you make asynchronous calls using Async method , the thread will not wait for I/O operation to finish and will perform other task just appearing to be parallel or multi threaded.

Q8. What is the use of the “using” block in C#?

When an object is created inside a using block, the C# compiler automatically generates code to ensure that the object’s Dispose method is called when the block is exited. This ensures that the object’s resources are released and any cleanup tasks are performed, even if an exception is thrown.

To allow any class to have dispose method, you need to implement IDisposable interface for that class.

By ensuring that objects are properly disposed of, the using block helps prevent resource leaks and other problems that can arise from not cleaning up after objects that use
unmanaged resources.

Q9 . What is the difference between Action and Func?

Action and Func are built in delegates in .Net Library that can be used instead of creating new custom delegate in the code. Prior to the introduction of Action and Func, developers had to create custom delegate types for methods with specific signatures. This led to the creation of numerous delegate types, increasing the amount of boilerplate code.

Action and Func (or any other delegate for that matter) allows developers to pass functions as arguments, return functions from other functions, and use lambda expressions more effectively.

  • Action is a delegate type that represents a method that takes zero or more parameters but does not return a value (void).
  • Func is a delegate type that represents a method that takes zero or more parameters and returns a value.

Leave a Reply

Your email address will not be published. Required fields are marked *