Search This Blog

Wednesday, June 9, 2021

Q36-Q40

Q36. What is the difference between the Accept header and the Content-Type header?
Q37. What is Domain Driven Architecture? 
Q38. 
Q39. 
Q40. 

===============================================================================
Q36. What are the differences between accept header and content-type header in sense of content negotiation?

Answer:
In Web APIs, both Accept and Content-Type are HTTP headers, but they serve different purposes:
1. Accept Header
Accept: application/json
means “I want the response in JSON.”

2. Content-Type Header
Describes the format of the actual body being sent.
Content-Type: application/json
means “The body I’m sending you is JSON.”
 
===============================================================================
Q37. What is Domain Driven Architecture? 

Answer:
  • Most moderns ORM follow domain driven architecture like entity framework. 
  • They have domain classes which are actually POCO (plain old C# object) classes which are entity classes 
  • We usually use Repository pattern with Domain Driven architecture. 


===============================================================================
Q38. 

===============================================================================
Q39. 


===============================================================================

===============================================================================

Sunday, June 6, 2021

Q31-Q35

Q31. Give real life example of abstract class usage in your project?
Q32. What is dictionary?
Q33. How to sort elements in dictionary by value? 
Q34. What are delegates? How it is related to lambda expression. 
Q35. What is event driven programming?

===================================================================================
Q31. Give real life example of abstract class usage in your project?

Answer:
In my project we have a abstract repository Generic class so that all the repositories will have basic functionality of repository and they need not to implement again and again unless required.

public abstract class Repository<T> 
{
    //some simple methods, which will be called from all repository

public void InsertonSubmit(T entity)
{
//
}

//some virtual methods, which can be changed if required
public virtual void UpdateonSubmit(T entity)
{
//
}

//Some abstract method, which always depend on derived class but we want to enforce the //remembrance of writing logic

public abstract void ChangeSessionValues(T entity)

// child class will give definition.
}


===================================================================================
Q32. What is dictionary?

Answer:
public class Dictionary<TKey,TValue> 

Dictionary<string, string> openWith =     new Dictionary<string, string>();
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");

Console.WriteLine(openWith["bmp"]);

===================================================================================
Q33. How to sort elements in dictionary by value? 

Answer:
Dictionary is a collection thus linq and lambda can be applied on it. 

var ans = openWith.orderby(key=>key.value

===================================================================================
Q34. What are delegates? how it is related to lambda expression

Answer:
Delegates are pointer to the function. Delegates are of two types 1. Singlecast and 2. Multicast. 

Lambda expression - is an anonymous method that can be used to create delegates. 
When you create delegates you can pass method name as a parameter to delegate object. 

  1. using System;  
  2. namespace DAL {  
  3.     public delegate void MyDelegate();  
  4.     class Program {  
  5.         public void CallerOne() {  
  6.             Console.WriteLine("This is Caller One function...");  
  7.         }  
  8.         static void Main() {  
  9.             //Create object of the class and give it an instance  
  10.             Program p = new Program();  
  11.             //Crete the object of delegate and note that you have to pass the method as parameter  
  12.             MyDelegate myDelegate = new MyDelegate(p.CallerOne);  
  13.             myDelegate();  
  14.         }  
  15.     }  
  16. }   

Multicast Delegate
  1. using System;  
  2. namespace DAL {  
  3.     public delegate void MyDelegate();  
  4.     class Program {  
  5.         public void CallerOne() {  
  6.             Console.WriteLine("This is Caller One function...");  
  7.         }  
  8.         public void CallerTwo() {  
  9.             Console.WriteLine("This is Caller Two function...");  
  10.         }  
  11.         public static void CallerThree() {  
  12.             Console.WriteLine("This is Caller Three function...");  
  13.         }  
  14.         static void Main() {  
  15.             //Create object of the class and give it an instance  
  16.             Program p = new Program();  
  17.             //Crete the object of delegate and note that you have to pass the method as parameter. This is Single Cast  
  18.             MyDelegate myDelegate = new MyDelegate(p.CallerOne);  
  19.             //Crete the object of delegate and note that you have to pass the method as parameter. This is Single Cast  
  20.             MyDelegate myDelegate1 = new MyDelegate(p.CallerTwo);  
  21.             //Crete the object of delegate and note that you have to pass the method as parameter. This is Single Cast  
  22.             MyDelegate myDelegate2 = new MyDelegate(CallerThree);  
  23.             myDelegate();  
  24.             myDelegate1();  
  25.             myDelegate2();  
  26.             //This Idea is Called Multi Cast  
  27.             MyDelegate MyDelegate_MultiCast_Idea = myDelegate + myDelegate1 + myDelegate2;  
  28.             MyDelegate_MultiCast_Idea();  
  29.         }  
  30.     }  

Lambda Expression -- It reduces the reference to the method name part. as it become the part of delegate itself as an anonymous method. 

  1. using System;  
  2. namespace DAL {  
  3.     public delegate void MyDelegate();  
  4.     class Program {  
  5.         static void Main() {  
  6.             //Here '=>' thing work to implement Labda Expression. It can be further dicsussed but I will discuss this in future article.  
  7.             MyDelegate myDelegate = new MyDelegate(() => {  
  8.                 Console.WriteLine("This is Caller One function...");  
  9.             });  
  10.             myDelegate();  
  11.         }  
  12.     }  
  13. }   

===================================================================================
Q35. What is event driven programming?

Answer:
As the name suggest this technique uses events as base for developing the software. We can have a software which is running without any interaction with events but usually these days software are developed with event interaction. events could be mouse click, uploading a video, visiting a new page etc. 

===================================================================================

Thursday, March 4, 2021

Q26-Q30

Q26. What is the difference between Dispose and Finalize keyword in C#?
Q27. What are the main components of CLR in C#?
Q28. Difference between CTS and CLS?.
Q29. How to implement IDispose in C#? give rough code.
Q30. How to implement Finalize in C#? give rough code.
---------------------------------------------------------------------------------------------------------------------------------
Q26. What is the difference between Dispose and Finalize keyword in C#?

Answer:
When the .NET framework instantiates an object, it allocates memory for that object on the managed heap. The object remains on the heap until it's no longer referenced by any active code and then it will be garbage collected by GC. 

.NET Framework provides two methods Finalize and Dispose for releasing unmanaged resources like files, database connections, COM etc. This article helps you to understand the difference between Finalize and Dispose method.


A Dispose method should call the GC.SuppressFinalize() method for the object of a class which has destructor because it has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method.


Dispose
Finalize
It is used to free unmanaged resources like files, database connections etc. at any time.
It can be used to free unmanaged resources (when you implement it) like files, database connections etc. held by an object before that object is destroyed.
Explicitly, it is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface.
Internally, it is called by Garbage Collector and cannot be called by user code. 
Although we can do is call destructor. 
It belongs to IDisposable interface.
It belongs to Object class.
It's implemented by implementing IDisposable interface Dispose() method.
It's implemented with the help of destructor in C++ & C#.
There is no performance costs associated with Dispose method.
There is performance costs associated with Finalize method since it doesn't clean the memory immediately and called by GC automatically.


---------------------------------------------------------------------------------------------------------------------------------
Q27. What are the main components of CLR in C#?

Answer:
CLR - Common Language Runtime has below 4 main components
  1. CLS - Common language Specification
  2. CTS - Common Type specification
  3. GC - Garbage collector
  4. JIT - Just in Time compiler. 

---------------------------------------------------------------------------------------------------------------------------------
Q28. Difference between CTS and CLS?

Answer:
Both are used for cross language communication and type safety. 

CTS:
It defines rules that every language must follow which runs under .NET framework. It ensures that objects written in different .NET languages like C#, VB.NET, F# etc. can interact with each other. This is more about type safety. 

CLS:
It defines a set of rules and restrictions that every language must follow which runs under .NET framework. This is more about communication. 

For example, one rule is that you cannot use multiple inheritance within .NET Framework. As you know C++ supports multiple inheritance but; when you will try to use that C++ code within C#, it is not possible because C# doesn’t supports multiple inheritance.

---------------------------------------------------------------------------------------------------------------------------------
Q29. How to implement IDispose in C#? give rough code.

Answer:

Implementing Idisposable
public class MyClass : IDisposable
{
 private bool disposed = false;
 
 //Implement IDisposable.
 public void Dispose()
 {
 Dispose(true);
 GC.SuppressFinalize(this);
 }

 protected virtual void Dispose(bool disposing)
 {
 if (!disposed)
 {
 if (disposing)
 {
 // TO DO: clean up managed objects
 }
 
 // TO DO: clean up unmanaged objects

 disposed = true;
 }
 }
}
--------------------------------------------------------------------------------------------------------------------------------
Q30. How to implement Finalize in C#? give rough code.

Answer:
Finalize keyword usage

// Using Dispose and Finalize method together
public class MyClass : IDisposable
{
 private bool disposed = false;
 
 //Implement IDisposable.
 public void Dispose()
 {
 Dispose(true);
 GC.SuppressFinalize(this);
 }

 protected virtual void Dispose(bool disposing)
 {
 if (!disposed)
 {
 if (disposing)
 {
 // TO DO: clean up managed objects
 }
 
 // TO DO: clean up unmanaged objects
 
 disposed = true;
 }
 }
 
 //At runtime C# destructor is automatically Converted to Finalize method
 ~MyClass()
 {
 Dispose(false);
 }
}

Wednesday, February 17, 2021

Microservices

 Microservices
Its an architectural design that structures the application as a collection of services that are loosely coupled and independently deployable. 

Friday, February 12, 2021

Q21-Q25

Q21. How we can assign an alias to any webapi action method?
Q22. What is the use of LET keyword in linq queries?
Q23. Tell the syntax of inner join and left join in linq queries?
Q24.  How to select top 5 records using linq queries. tell me the syntax
Q25. Difference between web service, web api and WCF?
===========================================================================
Q21. How we can assign an alias to any webapi action method?

Answer:
ActionName attribute is an action selector which is used for a different name of the action method. 

[ActionName("AliasName")]

example
 public class HomeController : Controller{
      [ActionName("ListCountries")]
      public ViewResult Index(){
         ViewData["Countries"] = new List<string>{
            "India",
            "Malaysia",
            "Dubai",
            "USA",
            "UK"
         };
         return View();
      }

===========================================================================
Q22. What is the use of LET keyword in linq queries?

Answer:
With the help of LET keyword we can utilize the assigned value to the next statement.
for example. in below example avgSalary is calculated on fly to compare it with column emp.salary. 


one more example:

===========================================================================
Q23. Tell the syntax of inner join and left join in linq queries?

Answer:
Inner Join


Left Join
In order to perform the left outer join using query syntax, you need to call the DefaultIfEmpty() method on the results of a group join.

Step1: The first step to implement a left outer join is to perform an inner join by using a group join

Step2: we need to call the DefaultIfEmpty() method on each sequence of matching elements from the group join.
===========================================================================
Q24.  How to select top 5 records using linq queries. tell me the syntax

Answer:
Using orderby and then 'Take' 

var list = (from t in ctn.Items  
       where t.DeliverySelection == true && t.Delivery.SentForDelivery == null  
       orderby t.Delivery.SubmissionDate  
       select t).Take(5);  

===========================================================================
Q25. Difference between web service, web api and WCF?

Answer:
Webapi is a type of webservice. All webapi is a webservice but not all webservice are webapis
Webservices are of two type SOAP and REST. 

Web service is something is provided over the web but it is different from web application. it work response and request but not on User interface. 

WebService - usually SOAP based
1. Simple object access protocol.
2. It is purely XML based
3. Data can be transferred over http, smtp, ftp etc
4. Performance is less as compared to REST 
5. Webmethods [web method] were used as an attribute to methods to make webservice enabled. 

WebAPI- only REST based
1. Representational state transfer protocol. 
2. It is based on both XML and JSON. 
3. Transfer data over http. 
4. Performance is good as compared to SOAP. 
5. It can be hosted within application or on IIS. 

WCF
1. It is SOAP based and return only XML. 
2. It is the evolution of the web service(ASMX) and support various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ.
3. The main issue with WCF is, its tedious and extensive configuration.
4. It can be hosted with in the application or on IIS or using window service.