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

Wednesday, 11 January 2012

How does Window execute a .net exe file?

Every .exe or .dll file apart from containing the metadadata table and the IL code also contain a PE32(32+) header thata deteremines what kind of machine or processor this exe is targetting for(32 bit,64 bit etc...)

After the windows has examined what processor is needed for this exe from the header of the exe it will try to load the corrosponding version(X86,X64 or IA64)mscoree.dll to the address space of the process.For a 32 bit system this dll is found in C:\Windows\System32 for a 64 bit system this is found in the same directory for backword compatibility.

The processor primary thread than call a method defined inside mscoree.dll.This method will initialize the CLR,executes the exe assembly and calls the main method.

At this point the managed application is up and running.

How to bind a drop down list with an xml with the items of the dropdown list being sorted?

Lets assume we have an XML file which looks like this
<Sates>
<State>
<ID>1</ID>
<Name>Orissa</Name>
  </State>

<State>
<ID>2</ID>
<Name>Karnataka</Name>
</State>

<State>
<ID>3</ID>
<Name>Kolkata</Name>
</State>

<State>
<ID>4</ID>
<Name>A.P.</Name>
</State>

<State>
<ID>5</ID>
<Name>Bihar</Name>
</State>
</Sates>

To bind this xml with a dropdown list(say "ddlXML") we have to bind this xml with a dataset.
DataSet dsXML = new DataSet();

dsXML.ReadXML(Server.MapPath("~/XMLFiles/State.xml"))(the xml file State.xml is present inside XMLFiles folder inside project directory)

Create a DataView to out of this DataSet that will provide the sorting option and than bind this DataView with the dropdown

DataView dvXML = dsXML.Tables[0].DefaultView();
dvXML.Sort = "Name";

Now bind this DataView with the dropdown list
ddlXML.DataSource = dvXML;

ddlXML.DataBind();

DataView can be accessed through a DefaultView property on DataTable. DataView class allows us to sort and filter data but not store it.We can also use the DataView class to sort filtered inputted data to the database.

Happy Coding :)

Sunday, 8 January 2012

Explain CLR

The heart to the .net frame work is its runtime execution environment that is called CLR or Common Language Runtime.As the name suggests it is a common runtime being used by variety of programming languages.The common features of CLR ie memory management,security,assembly loading,error handling is available to all languages that target the runtime.For example runtime uses "exception" to report errors.Thus all the languages that target runtime use exception to report errors. The code that runs under CLR is called managed code.

how ever before running under CLR any code that we write has to be compiled. And this compilation is a two step process in .net.
>Compilation of the source code to the microsoft intermediate language code MSIL(by the corresponding language compiler)
>compilation of il to machine specific code by the clr
This two stage compilation is very important because many of the .net key features is being provided by the intermediate language.IL is a low-level language that can be easily converted to the machine specific language.Having an universal syntax for the IL code gives the following advantage
Platform Independence
This means the same file containing the byte code instruction can be placed on any platform,the final stage of compilation can be easily accomplished so that it can run on that particular platform.Hence compiling the .net code to IL we achieve the same platform independence that compiling to java byte code,java has achieved.However complete platform independence for .net is theoretical.
Performance Improvement
IL is just in time compiled.Instead of compiling the entire application in one go JIT compiles each portion of the code as it is called.When a code is compiled the native executable is stored until the application exits so that it does not need to be recompiled the next time this portion of the code is executed.
Microsoft argues that this process is more efficient than compiling the entire code at the start of the application because a large portion of the code is not executed at any given run.This explains why the execution of managed IL is as fast as executing the native machine code.
But what Microsoft explains for the real performance improvement is:as the final stage of compilation takes place at runtime JIT is aware of what processor type the program will run on.that means it can optimize the final executable to take the advantage of any feature that the processor provides
Language Interoperability
We can compile to IL from one language and this code can be inter-operable with the code that has been compiled to IL from another language.

Tuesday, 27 December 2011

how to export your grid data to an excel sheet?

Here is a line by line explanation of the complete code for exporting the data grid to an excel sheet.
Here we'll not consider the pagination and will export th entire grid data. The point pagination is not considered is because in most of the cases while exporting to excel it is needed to display the entire data rather a paricular portion of it(which of course makes no sense).
For this shake we'll not depend on the grid that is currently in view in the page(if at all it is,here i am asuming that there is a grid in your ui and you want to export it to an excel).
Hence we'll create a grid on the fly provide it with the style that we wanted to be displayed in the excel and bind it with the complete data-source.

What is .net and what is there inside it?

.net is nothing but just a frame work that provides developers a framework to develop windows form,web application using variety of languages like c#,vb,pearl,python and more others. C# and VB are the most popularly used languages with .net framework.
.net is consisting of the following four important components

>.net frame work base classes
>common language runtime

the .net base classes are the common class libraries that can be used by all the .net compliant languages to achieve some common tasks.

CLR being heart of the .net frame work serves for most of the .net feature s like exception handling,garbage collection and also it contributes towards the platform independence behavior feature of .net.