How does Garbage collector work ?

Garbage collector serves as a automatic memory manager. It allocates memory for objects on managed heap. The heap is divided into three generations, generation 0, generation 1 and generation 2 , so it can handle short-lived and long-lived objects. A temporary variable is an example of short-lived object and an object in server application with static data is an example of long lived object.

Generation 0 is youngest generation and contains short-lived objects. All newly created objects are contained in generation 0. Garbage collection occurs more frequently in this generation. If there is no memory in generation 0, they are promoted to generation 1.

Generation 1 acts as a buffer between short-lived and long-lived objects, it contains short-lived objects.

Generation 2 contains long lived objects.

A garbage collection has the following phases:
  • A marking phase will find and create a list of all live objects.
  • A relocating phase will update the references to the objects that will be compacted.
  • A compacting phase will reclaim the space occupied by the dead objects and will compact the surviving objects. The compacting phase will move objects that have survived a garbage collection toward the older end of the segment.
Collecting a generation means collecting objects in that generation and all its younger generations.

What is managed code and unmanaged code?

Managed code is not compiled to machine code but to an intermediate language which is interpreted and executed by some service(Common Language Runtime in case of .NET) on a machine and is therefore operating within a secure framework which handles dangerous things like memory and threads for us.

Unmanaged code is compiled to machine code and therefore executed by the Operating System directly.

What is Common Table Expression in MS SQL Server?

We can think of common table expression as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. It lasts only for the duration of the query and can be referenced multiple times in the same query.

What is linked server?

Linked Servers allows us to connect to other database instances on the same server or on another machine or remote servers. It allows SQL Server to execute SQL scripts against OLE DB data sources on remote servers using OLE DB providers. 
After setting up the Linked Servers we can easily access the other server tables, procedures etc. 

Once the connection is established, we can start with the CRUD operations. The advantage is about security; its works on Windows as well as SQL Server Authentications. 

In Sql Server we refer as linked servers and in Oracle we refer them as database links.


What is use of temp data?

Temp data in MVC is used to store temporary data which can be used in subsequent request. Temp data is cleared out once it is read in any other request. To preserve the Temp data for subsequent requests we can use of  TempData.Keep().

What is stored procedure

A stored procedure is a set of Structured Query Language (SQL) statements with an assigned name, which are stored in database as a group, so it can be used and shared by multiple programs any number of times.

What is the difference between Var and Dynamic in C#?

“Var” is statistically typed,this means type of variable declared is decided by compiler at compile time.This type of variables should be initialized at the time of declaration.

“Dynamic” is dynamically typed ,this means the type of variable declared is decided by compiler at run time.This type of  variables are need not be initialized.

difference between varchar and nvarchar

Nvarchar stores UNICODE data. If you have requirements to store UNICODE or multilingual data, nvarchar is the choice. Varchar stores ASCII data and should be your data type of choice for normal use.