Search This Blog

Q41-Q45

Q41. What is Yield in C#? How IEnumerator is different from Yield?
Q42. What are the benefits of using linq queries over stored procedures?
Q43. What are the benefits of using Stored procedure over linq?
Q44. Difference between Ienumerable and Ienumerator in C#?
Q45. Difference between Boxing and Unboxing?
=========================================================================
Q41. What is Yield in C#? How IEnumerator is different from Yield?

Answer:
Yield keyword allows you to iterate over collection of items once at a time. It gives state management and logic to move next element when required. 
There are two main forms of Yield keyword
1. yield return: use to return each element when called. Must be called inside loop to yield all elements. 
2. yield break: Used to end iteration early. 
=========================================================================
Q42. What are the benefits of using linq queries over stored procedures?

Answer:
1. Debugging: Linq being part of .net debugger. It is better to conclude in one debugger than moving between two debuggers. 
2. Deployment: Since LINQ is part of .NET code. we do not need to worry about two different deployments. 
3. Type Safety: LINQ are type safe and thus you will get most of the errors during the compile time rather than waiting for run time. 

In short LINQ increases the productivity and reduces the development time. 
=========================================================================
Q43. What are the benefits of using Stored procedure over linq?

Answer:
1. Stored procedure uses usually different server resource and can give more performance. 
2. Complex queries and big stored procedures are usually preferred as they are pre compiled code and takes less time in comparison to linq. 

=========================================================================
Q44. Difference between IEnumerable, IQueryable and IEnumerator in C#? 

Answer
Both IEnumerable and IEnumerator  servers to iterate over collection of items. 

IEnumerable internally uses IEnumerator. IQueryable internally uses and extend IEnumerable

If we want to iterate through the list (or any collection) at one time, then IEnumerable is the best choice, but if we want to preserve the state of an iteration variable, then we should go with IEnumerator.

IEnumerator has two methods MoveNext() and Reset(). It also has a property called Current. MoveNext() return boolean. it is false if end of list is reached. 

Key Differences:
  1. Iteration Control:
    1. IEnumerable: Simplifies Iteration using ForEach. Foreach uses IEnumberalbe behind the scene. 
    2. IQueryable: Its a Queryable command to be executed on database side. 
    3. IEnumerator: Provides manual control like MoveNext(), Reset() over iteration. 
  2. Execution Context:
    1. IEnumerable: Executes In-Memory
    2. IQueryable: Executes query on datasource side. Transfer LINQ queries to SQL queries
    3. IEnumerator: Manages state of Iteration. 

=========================================================================
Q45. Difference between Boxing and Unboxing?

Answer:
  • Boxing is the conversion of the value type to an Reference(object) type.
  • Unboxing refers to the conversion of the Reference(object) type to the value type
example:
int i = 24;
object ob = i; // Box the integer type n into object type ob.
int j = (int) ob; // Unbox the integer value stored in object type ob to integer type y


No comments:

Post a Comment