1. What do you mean by Garbage Collection?
Garbage collection is a CLR feature which automatically manages memory. Programmers forget to release the objects while coding laziness (Remember in VB6 where one of the good practices is to set object to nothing). CLR automatically releases objects when they are no longer referenced and in use.CLR runs on non-deterministic to see the unused objects and cleans them. One side effect of this non-deterministic feature is that we cannot assume an object is destroyed when it goes out of the scope of a function. Therefore, we should not put code into a class destructor to release resources.
2. What is the use of Finalize method in .NET?
.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (ex: - Windows API created objects, File, Database connection objects, COM objects etc) is outside the scope of .NET framework we have to explicitly clean our resources. For these types of objects .NET framework provides Object.Finalize() method which can be overridden and clean up code for unmanaged resources can be put in this section.
3. How can we force Garbage Collector (GC) to run?
System.GC.Collect() forces garbage collector to run. This is not recommended but can be used if a situation arises.
4. What is the use of Dispose method?
Dispose method belongs to IDisposable interface. So if any object wants to release its unmanaged code best is to implement IDisposable and override the Dispose method of IDisposable interface. Now once your class has exposed the Dispose method it’s the responsibility of the client to call the Dispose method to do the cleanup.
public class SimpleCleanup : IDisposable
// some fields that require cleanup
private SafeHandle handle;
private bool disposed = false; // to detect redundant calls
this.handle = /*...*/;
protected virtual void Dispose(bool disposing)
if (handle != null)
disposed = true;
public void Dispose()
5. How can we suppress a finalize method?
By using: GC.SuppressFinalize();
6. What is an ArrayList?
Array whose size can increase and decrease dynamically. Arraylist can hold item of different types. As Arraylist can increase and decrease size dynamically you do not have to use the REDIM keyword. You can access any item in array using the INDEX value of the array position.
7. What is a HashTable and what are the differences between HashTable and ArrayList?
You can access array using INDEX value of array, but how many times you know the real value of index. HashTable provides way of accessing the index using a user identified KEY value, thus removing the INDEX problem.
Array list and hash table both use to save the data but hash table is faster than ordinary array list. In hash table there is combination of key and value like Dictionary. So the value can be find easily with the Key if we are having.
HashTable: The System.Collections namespace provides a HashTable container. A HashTable represents a key/value pair in which the key is used for fast lookup.
Hashtable wds = new Hashtable();
We add the key/value pair to a Hashtable either by an explicit assignment:
wds[ key ] = 1;
wds.Add( key, 1 );
ArrayList: This is also coming under System.Collections namespace. The ArrayList class is a designed replacement for an unadorned array.
ArrayList myAL = New ArrayList();
We add values as below:
HashTable is faster than ArrayList.
8. What is the difference between Array and Arraylist?
As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize() or by setting the Capacity property explicitly.
9. What are different types of Assembly?
There are two types of assembly Private and Public assembly.
- A private assembly is normally used by a single application, and is stored in the application's directory, or a sub-directory beneath.
- A shared assembly is normally stored in the global assembly cache, which is a repository of assemblies maintained by the .NET runtime. Shared assemblies are usually libraries of code which many applications will find useful, e.g. Crystal report classes which will be used by all application for Report
10. What is NameSpace?
Namespace has two basic functionalities:-
- NameSpace Logically group types.Example System.Web.UI logically groups our UI related features.
- In Object Oriented world many times it is possible that programmers will use the same class name. By qualifying NameSpace with classname this collision can be removed.
11. What is Difference between NameSpace and Assembly?
Following are the differences between namespace and assembly:
- Assembly is physical grouping of logical units. Namespace logically groups classes.
- Namespace can span multiple assemblies.
12. What is Intermediate Language Dissembler (ILDASM)?
ILDASM basically converts the whole exe or dll in to IL code.
13. What is Manifest?
Assembly metadata is stored in Manifest. Manifest contains all the metadata needed to do the following things( See Figure Manifest View for more details) :
- Version of assembly
- Security identity
- Scope of the assembly
- Resolve references to resources and classes.
The assembly manifest can be stored in either a PE file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a stand-alone PE file that contains only assembly manifest information.
14. Where is version information stored of a assembly?
Version information is stored in assembly in manifest.
15. What is private and shared assembly?
The assembly which is used only by a single application is called as private assembly. Suppose you created a DLL which encapsulates your business logic. This DLL will be used by your client application only and not by any other application. In order to run the application properly your DLL must reside in the same folder in which the client application is installed. Thus the assembly is private to your application.
Suppose that you are creating a general purpose DLL which provides functionality which will be used by variety of applications. Now, instead of each client application having its own copy of DLL you can place the DLL in 'global assembly cache'. Such assemblies are called as shared assemblies.
16. What is Global Assembly Cache (GAC) and what is the purpose of it?
Each computer where the common language runtime is installed has a machine-wide code cache called the global assembly cache. The global assembly cache stores assemblies specifically designated to be shared by several applications on the computer. You should share assemblies by installing them into the global assembly cache only when you need to perform the below steps.
1). Create a strong name using sn.exe tool eg: sn -k keyPair.snk
2). Within AssemblyInfo.cs add the generated file name eg: [assembly: AssemblyKeyFile("abc.snk")]
3). Recompile project, then install it to GAC by either drag & drop it to assembly folder (C:\WINDOWS\assembly OR C:\WINNT\assembly) (shfusion.dll tool) or gacutil -i abc.dll
17. How do I create shared assemblies?
Following steps are involved in creating shared assemblies:
1) Create your DLL/EXE source code
2) Generate unique assembly name using SN utility
3) Sign your DLL/EXE with the private key by modifying AssemblyInfo file
4) Compile your DLL/EXE
5) Place the resultant DLL/EXE in global assembly cache using AL utility
18. What is a satellite assembly?
Multilingual applications in .NET to support multilingual functionality which are customized for localization, These assemblies are called as distribute these assemblies separately than the core modules.
19. How we use Try-Catch-Finally blocks for handling exceptions in .NET?
This is also called Structured Exception Handling (SEH). When an exception occurs, the system searches for the nearest catch clause that can handle the exception, as determined by the run-time type of the exception. First, the current method is searched for a lexically enclosing try statement, and the associated catch clauses of the try statement are considered in order. If that fails, the method that called the current method is searched for a lexically enclosing try statement that encloses the point of the call to the current method. This search continues until a catch clause is found that can handle the current exception, by naming an exception class that is of the same class, or a base class, of the run-time type of the exception being thrown. A catch clause that doesn't name an exception class can handle any exception.
Once a matching catch clause is found, the system prepares to transfer control to the first statement of the catch clause. Before execution of the catch clause begins, the system first executes, in order, any finally clauses that were associated with try statements more nested that than the one that caught the exception.
Exceptions that occur during destructor execution are worth special mention. If an exception occurs during destructor execution, and that exception is not caught, then the execution of that destructor is terminated and the destructor of the base class (if any) is called. If there is no base class (as in the case of the object type) or if there is no base class destructor, then the exception is discarded.
Enclose code that accesses files, databases, and so forth inside a try-catch block because access to those resources might be denied due to various reasons causing an exception. The third part of this block is finally. It is executed irrespective of the fact that an exception has been raised. Hence, use the finally block to complete the housekeeping jobs.
As a good programming practice, always catch specific exceptions. To view the exception types supported by the .NET Framework, use the Debug menu and select Exceptions in Visual Studio.NET.
In the following code, you try to access a table that does not exist in the Northwind database; therefore, an exception is raised. By using the try catch and finally block, you handle the exception and display a message
con = new SqlConnection("integrated security=SSPI;
data source= (local);persist security info=False;
da = new SqlDataAdapter("Select * from TblNotExisits", con);
ds = new DataSet();
return "Connection Unsuccessful " + ex.Message;
return "Connection Successful";
20. What is difference between System Exceptions and Application Exceptions?
All exception derives from Exception Base class. Exceptions can be generated programmatically or can be generated by system. Application Exception serves as the base class for all application-specific exception classes. It derives from Exception but does not provide any extended functionality. You should derive your custom application exceptions from Application Exception. Application Exception is used when we want to define user defined exception. While System Exception are all which are defined by .NET.
21. What is Code Access Security (CAS)?
CAS is part of .NET security model that determines whether or not a piece of code is allowed to run and what resources it can use while running. Example CAS will allow a application to read but now write and delete rights are given to the application.
22. What is the use of Indexers?
- Identified by its signature.
- Accessed through an element access.
- Must be an instance member.
- A get accessor of an indexer has the same formal parameter list as the indexer.
- A set accessor of an indexer has the same formal parameter list as the indexer, in addition to the value parameter.
accessModifier returnType this[IndexType1 indexName1, IndexType2 indexName2, IndexType3 indexName3, .....]
//use indexName1, indexName2, indexName3 .. to get data
//use indexName1, indexName2, indexName3 .. to set data