Monday, 26 August 2013

Private Constructor in c#

What is a private Constructor?
At the very out set a private constructor is a constructor with an access specifier "private".

Class Employee                  
{
     private Employee()
     {
     }
}

As simple as it is.
 Now there are two important characteristics of a private constructor or we can say a class with a private constructor.

A class with a private constructor

1>Can not be inherited:
Eg: in context of the above example if we try something like:
Class Programmer : Employee
{

}
 The above code throw compile time exception.

2>Can not be instantiated:
Eg: Employee obj = new Employee() is not possible.

Why then we use private constructor?
There are many utility classes we use in our project. Utility classes contain functions that can be used across different files. In such scenarios we are not always interested in creating the object and using those classes. If we have a class with private constructor and the utility methods are static in nature than we can call those method with out instantiating a class. As instantiating class would require memory allocation for all members we are saved for those un-necessary memory foot prints.

public class Employee
{
       private Employee()
      {
      }
      public static void AddNewEmployee(){//...write your code here}
}

Main()
{
    //--Here we can call the "AddNewEmployee() method with out instantiating the object
   Employee.AddNewEmployee();
}



I hope this was easy to understand. In the next article I will try to explain why would we need a private constructor if there is a static class available.

Comments and suggestions are always welcome.

Monday, 14 January 2013

.Net Questions and answers

Q:Explain the fundamental architecture of .net frame work?


.Net frame work relies upon following 7 components.

CLR: Commonn Language Runtime, as the name suggests its a common runtime for all .net supported languages. The .net code does not directly talk to the OS. It is the CLR that seats above OS and make the communication between the .net code and OS happen. Any language targeting to CLR will enjoy all the features provided by it,e.g. Exception Handling,Security,Threading,Garbage Collection etc...

MSIL: The code that we write (in c#,vb or any other language) does not get directly compiled to the binary or machine code. The corresponding language compiler first converts it to an intermediate language called Microsoft Intermediate Language(MSIL) which is then converted to the machine code by the CLR. The MSIL produced from evru other language are same and hence this leads to interoperability among code written in various languages(c#,vb,j# etc...)

CLS: Common Language Specification(CLS) is a set of minimum guidelines that every language has to follow if they are targeting CLR.

JIT:  Just In Time compiler is invoked by CLR while trying to compile the MSIL. Unlike any tradional compiler it doesnt compile the entire code at one go and rather compile only that portion of the code which is curently under execution. Once a code is compiled it is not compiled again and again unless some changes made to it. Hence next time when the same piece of code needs to be compiled again it'll get the same from memory and thus reducing the compilation time and increasing effiiency.

CTS: Common Type Specification is a common set of specification for various types (data type) that all .net languages have to comply with. Different languages have different ways to implement a data type, but they have tyo follow this CTS.

Microsoft Class Library: In order to make the developer's life easy microsft has a provided a huge class library(almost 13000 classes) with .net for common functionalities like I/O operation, Graphics feature, Threading etc...

Garbage Collection: Garbage Collection is an efficient way of memory management which does not rely on the user to explicitly free up un-used memory. When ever a certain memory remain un-used for a long period time .net calls the garbage collector and it will release that memory for further use.


Explain the Role of CLR in .net framework?
Ans: The Common Language Runtime(CLR) being a part of the .net framework provide an execution enviroment for the code written by .net targetted languages.
The corresponding language compiler compiles the code into intermediate language(MSIL) to a portable executable file (PE32 or PE64 type).
Than CLR takes care of converting this IL to CPU specific instruction by invoking the JIT.
The Language along with the enviroment writes some more information to the PE for the CLR to take care while executing the IL. These are information like required CLR version , langauge being used and its version etc...These information are stored in a metadata table