Search This Blog

Q66-Q70

Q66.What is the difference between .first(), .single(), .firstorDefault in .net linq?
Q67. Difference between single and singleOrDefault in linq?
Q68. Different types of design patterns in C#?
Q69. Use of keyword Sealed in C#?
Q70. Disadvantages of late binding in C#?
-----------------------------------------------------------------------------------------------------------------------------
Q66. What is the difference between .first(), .single(), .firstorDefault in .net linq?

Answer:
var numbers = new List<int> { 10, 20, 30 };
var first = numbers.First();            // 10
var firstOrDefault = numbers.FirstOrDefault(); // 10
var single = numbers.Single();          // throws exception (more than one element)


1. .First() = If the sequence has at least one element, it returns the first one. No issues in having more than 1 element. If there are no elements it will throw error. 

2. .FirstOrDefault()n= Give me the first element, or the default value if there’s none. Default value is null for reference types and  0 for int, false for bool, etc.
it is ok for sequence to be empty. It wont throw the error but a default value. 

3. .Single() = Means the return is expected to be exact single element. if its more than 1 element it will through error. Eg fetching the particular employee ID. 

-----------------------------------------------------------------------------------------------------------------------------
Q67. Difference between single and singleOrDefault in linq?

Answer:
Both fetch the single element from the collection object or source, but Single will throw and exceptionif no element found, 

when SingleOrDefault will not throw any error, it will return the default value of that data type. Which is usually null. 

Means if you do not want exception to be thrown use. firstOrDefault() or SingleOrDefault()
-----------------------------------------------------------------------------------------------------------------------------
Q68. Different types of design patterns in C#?

Answer:
Creational, Structural, Behavioral 
  1. Behavioral - These design patterns are specifically concerned with communication between objects.
    1. Chain of responsibility design pattern. 
    2. Behaviour 
    3. Iterator 
  2. Structural - Make different pieces of systems in compatible with other part of System. 
    1. Adaptor
    2. Facade
    3. Proxy
  3. Creational - It deals with the object creation mechanism. 
    1. Singleton Design pattern
    2. Factory Design pattern
    3. Abstract Factory
-----------------------------------------------------------------------------------------------------------------------------
Q69. Use of keyword Sealed in C#?

Answer:
In c#, sealed is a keyword that is used to stop inheriting the particular class from other classes and we can also prevent overriding the particular properties or methods based on our requirements.

Real life usage. Lets say you have different subscription level in your product  eg basic, hd, fhd, uhd and u don't want uhd (ultra hd) to be further inherit by any other class u can make uhd as sealed class.  So from code reusability HD will be inherited from basic. fHD will inherit HD. And so on. every parent subscription will have all the features of its child subscription but there will be no further features above UHD as it is a sealed class.
-----------------------------------------------------------------------------------------------------------------------------
Q70. Disadvantages of late binding in C#?

Answer:
(1) program runs slow as it have to evaluate things at run time. 
(2) We don't have intelligence.
(3) We don't get error at compilation time. 
-----------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment