Friday, 28 September 2018

.Net Interview Questions for Experienced


What is an Assembly?
An Assembly is a basic building block of .Net Framework applications. It is basically compiled code that can be executed by the CLR. An Assembly can be executable (.exe) file or dynamic link library (DLL) file.
Assemblies contain the definition of types, versioning information for the type, meta-data and manifest.
There are two types of assemblies in .NET

1. Private Assembly
It is an assembly that is being used by a single application only. Suppose we have a project in which we refer to a DLL so when we build that project that DLL will be copied to the bin folder of our project. That DLL becomes a private assembly within our project. Generally the DLLs that are meant for a specific project are private assemblies.

2. Shared Assembly
Assemblies that can be used in more than one project are known to be a shared assembly. Shared assemblies are generally installed in the GAC. Assemblies that are installed in the GAC are made available to all the .Net applications on that machine.

GAC
GAC stands for Global Assembly Cache. It is a memory that is used to store the assemblies that are meant to be used by various applications. 

Difference between Manifest and metadata?
Manifest - Manifest maintains the information about the assemblies like version, name locale and an optional strong name that uniquely identifying the assembly. This manifest information is used by the CLR. The manifest also contains the security demands to verify this assembly.

Metadata - Metadata means data about the data. Metadata yields the types available in that assembly, like classes, interfaces, enums, structs, etc., and their containing namespaces, the name of each type, its visibility/scope, its base class, the interfaces it implemented, its methods and their scope, and each method’s parameters, type’s properties and so on

What is DLL?
A Dynamic Link library (DLL) is a library that contains functions and codes that can be used by more than one program at a time. Once we have created a DLL file, we can use it in many applications. To use DLL we need to add the reference of the DLL File. Both DLL and .exe files are executable program modules but the difference is that we cannot execute DLL files directly.

What is namespace?
Namespaces are used in C# to organize and provide a level of separation of codes. They can be considered as a container which consists of other namespaces, classes, etc.
A namespace can have following types as its members:
  1. Namespaces (Nested Namespace)
  2. Classes
  3. Interfaces
  4. Structures
  5. Delegates
Namespaces are not mandatory in a C# program, but they do play an important role in writing cleaner codes and managing larger projects.


Difference between managed and unmanaged code?
.NET supports two kind of coding
1) Managed Code
2) Unmanaged Code

Managed Code –

The code, which is developed in .NET framework, is known as managed code. This code is directly executed by CLR.
It offers services like garbage collection, run-time type checking, reference checking etc.
Example - class object

Unmanaged Code – 

Applications that are not under the control of the CLR are unmanaged.
Unmanaged Code managed by operation system.
Unmanaged code compiles straight to machine code and directly executed by the Operating System.
In .net framework, unmanaged code is executed with help of wrapper classes.
Example - files, network,database, binary image.
using(FileStream file = new FileStream(@"c:/file.txt", FileMode.Open))
{
       file.Close();
}

(Note – Here using statement handles Dispose of file object, so no need to call dispose() method.)


Difference between Dispose() and Finalize()?
.NET Framework provides two methods Finalize and Dispose for releasing unmanaged resources.

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.
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.

Note -
1.     It is always recommended to use Dispose method to clean unmanaged resources. You should not implement the Finalize method until it is extremely necessary.
2.     At runtime C#, C++ destructors are automatically Converted to Finalize method.
3.     You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.
4.     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.


Difference between assembly and namespace?

Assembly will contain Namespaces, Classes, Data types. it's a small unit of code for deployment. Assembly defines the name of the .dll file.It also avoids dll hell problem.

Namespace is used in order to avoid conflict of user defined classes.

Namespace
Assembly
Namespace is the logical naming decided at design time by the developer.
Scope for a particular type is defined at run time using Assembly.
Namespace contains set of unique names.
Assembly contains code of the form MSIL ( Microsoft Intermediate Language)
Classes available in your program will be logically grouped together under a namespace.
Logical units are physically grouped together as assembly.
Namespace can include multiple assemblies.
An assembly can contain types belonging to different namespaces.
Namespace doesn't have any classification.
Assembly can be classified as private assembly and public assembly. Private assembly is specific to a single application but shared/public assembly contains libraries which can be used by multiple applications.
Namespaces have to be mentioned in Project-Properties.
Assemblies need not be explicitly specified. They are automatically described in metadata and manifest files.
Namespaces can be nested. For example:
namespace sampleApp1 {
namespace SampleApp2 {
class sampleClass {

}
}
}
Such nesting is not permissible in assemblies.
Namespace is the one which you directly specify in your code. A simple example for namespace is shown below:
namespace sampleNamespace {
class sampleClass {
public void displayMsg() {
Console.WriteLine("In sampleClass");
}
}
}
In this example, sampleNamespace is the namespace you have created and sampleClass is within the scope of this namespace.
Creating an assembly on your own is not as simple as you create a namespace. If you want to package the code shown as an example for the namespace into an assembly, then you have to follow the steps shown below:
" Generate a key file inside the same folder where you have placed the code by typing the following statement in the command prompt:
sn -k sampleKey.key
" Sign your code component with this key. Assume that your code component is named as sampleClass.cs. Now use the following command:
csc sampleClass.cs /t:library /a.keyfile:sampleKey.key
" Place your signed assembly inside GAC using AL Utility:
AL /i: sampleClass.dll
" Now create a code that accesses the code in your assembly:
Using System;
Using sampleNamespace;
public class testClass {
sampleClass obj = new sampleClass();
obj.displayMsg();
}
" To test if your assembly got created and to test if the above code is working, compile the above code using the following statement:
csc testClass.cs /t:exe /r:<path of your assembly>
" Now if you execute testClass.cs, you should get the following output:
In sampleClass



Difference between usercontrol and custom control?

User Control
Custom Control
Designer associated with it.(have .aspx page).
Don’t have designer associated.
Re-usability web page.
Re-usability of control (or extend functionalities of existing control).
We can’t add to toolbox.
We can add to toolbox.
Just drag and drop from solution explorer to page(aspx).
Just drag and drop from toolbox.
You can register user control to.aspx page by Register tag.
You can register custom control to.aspx page by Register tag.
A separate copy of the control is required in each application.
A single copy of the control is required in each application.
Good for static layout.
Good for dynamics layout.
Easier to create.
Hard to create.
Not complied into DLL.
Compiled in to dll.
Here page (user page) can be converted as control then We can use as control in aspx.


Difference between String and StringBuilder?

String 
String is immutable, Immutable means if you create string object then you cannot modify it.
It always create new object of string type in memory.
 
 Example
 

 string str1 = "Hello Visitor";
 // create a new string instance instead of changing the old one
 str1 += "How Are";
 str1 += "You ??";
 
Stringbuilder 

StringBuilder is mutable, means if create string builder object then you can perform any operation like insert, replace or append without creating new instance for every time.it will update string at one place in memory doesnt create new space in memory.

Example
 

 StringBuilder sbValue = new StringBuilder("");
 sbValue.Append("Hello Visitor");
 sbValue.Append("How Are You ??");
 string strMyValue = sbValue.ToString();


What is Connection Pooling?
How to restrict the class instances creation? eg. restrict the class to create only 5 instances/objects