Saturday, August 18, 2007

Dot net framework - begineers

My goal is to give an introduction to Microsoft dot net framework and the terminologies.

Dot net framework is a development platform. You can write different type of components ( will explain in detail later )


  • Console application
  • Windows form application
  • Web application
  • Control Library
You may choose and pick a dot net programming language ( C# , VB.net, J#, etc ) of your own choice. I will be using C# ( pronounced as C-sharp ) in this tutorial for the code snippets.


IL
The output of the dot net code always results in IL ( intermediate language ).The compiler compiles the code into IL which is in the form of a dot net exe ( myprogram.exe ) or dll ( componenet.dll ) file. This file is called dot net assembly.


CLR
The main ingredients of the Dot net framework is CLR( common language runtime ). This is required to execute the code ( IL ) produced by compiler. The CLR converts the IL code into native code which is directly understood by the operating system. This conversion is done once and happens just in time ( JIT , i.e when the first time the program runs , also this conversion is saved for later executions)






Managed Code
IL code is sometimes referred to as managed code because CLR manages its execution.

Metadata
Metadata is always associated with a dot net assembly and is embedded in the same EXE/DLL file. This produced by the compiler at the time of IL code generation. To simply define metadata it contains a map of the IL code, this map defines various types ( classes , functions ,etc ) available in IL code.





Example

Lets quickly build a dumb dot net program that will open a form and pop up a message box with a "hello world" message.

Open Microsoft visual studio ( 2005 or 2003 ) and click on new project




Select C# as the language and 'Windows Application' as the application type and give a name to your project in the Name text box ( as shown in the picture above ) , then click on OK.









Visual studio will now generate a new windows forms project for you and present you with a blank form:




















You can now drag and drop controls form the toolbox onto this form.






Drag a button control from the toolbox on the left ( as shown ) onto the form.











Now double click on this button . Editor will directly take you to the button click event.



In the button_click function write the code MessageBox.Show("Hello World"); as shown below:




Congratulations ! Your first dot net program is ready now , hit the F5 key to run your program.


Some things to NOTE
The above example was the simplest possible win form example I could think of, sure you can go ahead and start writing your own programs which have a purpose and are smart. There are few things to note before that. Look at the set of files which visual studio generates when we create a new windows application project:



References
By default the project has some assemblies added as reference. The MessgeBox.Show method we used above is defined in the System.Windows.Froms assembly.



Program.cs
This file has some code which is automatically generated by visual studio. Double click on the file in solution expolorer to open it.





An important thing to note here is the Application.Run function and the argument supplied to this function.

Form1 : is the name of the class that defines the form.

Application.Run ( new Form1()) : creates an instance of your form as that instance passed to the Run function.



Run Function : this function is responsible to keep the application alive and in order to do so it infernally starts a GUI thread which keeps running till the application is not closed by the user and waits to take actions for any user interaction ( e.g. mouse clicks, key down events, painting, etc )



TO read more on C# symantics : http://www.codeproject.com/csharp/quickcsharp.asp?df=100&forumid=15737&select=2188586&msg=2188586