11. What are generations in Garbage collector (Ge 0, 1 and 2)?
Generation defines age of the object .
There are three generations:
Gen 0: When application creates fresh objects they are marked s Gen 0.
Gen 1: When GC is not able to clear the objects from Gen 0 in first round it moves them to Gen 1 bucket.
Gen 2: When GC visits Gen 1 objects and not able to clear them it moves them to gen 2.
Generations are created to improve the GC performances.
Garbage collector will spend more time on gen 0 objects rather than Gen 1 and Gen 2 thus improving the performance.
12. How do we clean unmanaged code?
Garbage collector only claims the managed code memory. For unmanaged code we need to put clean up logic manually in the destructor / finalize while coding.
13. Do using a destructor decreases the performances?
Yes, it does.
When we define a destructor , garbage collector does not collect these object in the first round.
It moves them to Gen 1 and then reclaims these objects in the next cycle.
As more objects are created in Gen 1 the performance of the application falls down because more memory is consumed.
14. How can we clean unmanaged objects also maintaining the performance?
To do this we need to do the following steps:
Implement IDisposable interface and implement the dispose function.
In Dispose function call the "GC.SuppressFinalize" method.
At the client side ensure that the "Dispose" function is called when the objects no more required.
Finalize and Dispose Pattern:
This ensures that your objects are created in the Generation 0 rather than the Generation 1..
"GC.SuppressFinalize" tells the garbage collector to not worry about destructor and destroy the objects in the first call itself.
Class clsMyClass: IDisposable
// In case the client forgets to call
// Dispose,destructor will be invoked for Dispose (false)
protected virtual void Dispose (bool disposing)
//Free managed objects.
// Free unmanaged objects
public void Dispose()
// Ensure that the destructor is not called
15. Can we force garbage collector to run?
"Syastem.GC.Collect()" forces garbage collector to run. This is not a recommended practice but can be used when any situation Arieses.
16. What is the difference between finalize and dispose?
Finalize is a destructor and dispose is a function which is implemented via IDisposable interface.
Finalize is non deterministic, since it's called by garbage collector. Dispose is a function and needs to be called by the client for the clean up.
Where finalize is automatically called by garbage collector while dispose needs to be called forcefully.
As a good practice finalize and dispose is used collectively because of double garbage collector loop.
17. What is CTS:
CTS - Common Type System
In .Net there are lots of languages like c#, VB.Net, VF.Net etc. There can be situations when we want code in one language to be called in other language.
In order to ensure smooth communication between these languages the most important thing is that they should have a common type system.
CTS ensures that the data types defined in two different languages get compiled to a common data type.
So, "Integer" data type in VB6 and "int" data type in C++ will be converted to System.int32, which is data type of CTS.
18. What is CLS?
CLS : Common Language Specification
CLS is a subset of CTS.
CLS is a specification or set of rules or guidelines.
When any programming language adheres to these set of rules it can be consumed by any .Net language.
For instance one of the rule which makes your application CLS non-compliant is when you declare your methods members with same name and with only case differences in C#.
You can try this, create a simple class in C# with same name with only case differences and try to continue the same in VB.NET, it will not work.
19. What is an Assembly?
Assembly is a unit of deployment like EXE or a DLL.
20. What are the different types of assembly?
There are two types of assembly:
Private - A private assembly is normally used by a single application, and is stored in application's directory, or a sub-directory beneath.
Public - A public or shared assembly is stored in the global assembly cache, which is a repository of assemblies maintained by the .Net runtime.
Shared assemblies are needed when we want the same assembly to be shared by various applications in the same computer.