Sunday, May 25, 2008

Stack and Heap in .Net Framework

Stack and Heap
While a program is running, its data must be stored in a memory. How much memory is required for an item, and where and how it is stored, depends on it type
A running program uses two regions of memory to store data. They are usually known as stack and heap.

Stack
System takes cares of all stack manipulation. As a programmer you need to worry about stack explicitly
But understanding stack will give a better hand on the way of programming. So it will help you to understand C# literature.. ( oops what a name ‘C# literature’.. ;) )
Stack is an array of memory that acts as a last-in, first-out (LIFO) data structure. Usually we store several kinds of data
· The values of certain types of variables
· The program’s current execution environment
· Parameters passed to methods

  • Additional Facts
    · Data can be added to and deleted from the top of the stack
    · Placing a data item at the top of the stack is called pushing item to the stack
    · Deleting an item from the top of the stack is called popping the item from the stack

Data items pushed on the top of the stack and popped from the top of the stack.

Pushing an integer “333” on the top of the stack moves the top of the stack up.

Heap
Heap is an area where chunks of memory can be allocated to store certain kinds of data.
No it’s not similar to Stack…
Unlike Stack, memory can be allocated and deallocated from heap in any order.

Theoretically we can say like that. Although our program can allocate memory from the heap, it cannot deallocate it. Instead the CLR’s Garbage Collector (GC) automatically cleans up orphaned heap objects when it determines that your code will no longer access them. This frees us from what in other programming languages can be an error-prone task.

This program has three allocated memory objects in the heap.

Later, one of this memory object is no longer used by the program.

The Garbage Collector (in .NET framework) finds the orphaned memory object


After garbage collection, the object’s memory has been released back to the heap

well that gives an idea of what stack and heap is...

Sunday, May 4, 2008

Basics of C# programming

Hmmm... Nobody likes hearing theory for long time… just taking a break…

Lets overview some sample codes.

To start with let’s see the structure of a basic C# program. Even if don’t catch up the code, just understand the terms, so that I can use those terms in explaining codes in detail.

Note I use Visual Studio 2008, with a black theme for Coding. (I don’t like the normal white background for many reasons… )

See this code.



The Output of this program will be like this. (Note this is just a simple program with Console output.)


Ok I will explain each line.



Line 1: using System;


  • This tells the compiler that this program uses types from the System namespace. This is a default namespace in .Net. I used this for getting the function WriteLine()


Line 2 : namespace SampleCodeBlogspot

  • I declared a new namespace for my program called SampleCodeBlogspot. Note that after declaring namespace, I put curly braces { }.


Line 4: class testApp



  • Declared a new class type for my program as testApp. Any members declared after this class inside curly braces, will automatically becomes members of this class.


Line 6 : static void Main()


  • In my program, Main is the only member of the testApp class.

  • Main is a special function used by the compiler as the starting point of the program.


Line 8 : Console.WriteLine(" Hello, this is from Midhun's Blogspot.");


  • My programs have only a simple statement which uses the Class Console in the namespace System, to print out the message to a window on the screen.

Note:

  • A C# program consists of one or more type declaration . In my sample program I have only one type. class testApp set

  • A namespace is a set of type declarations associated with a name. This sample Program creates a new namespace called SampleCodeBlogspot and uses a predefined namespace called System

  • Every C# program must have one class with a method ( or known as Function ) called Main. The starting point of execution of every C# program is at the first instruction in Main.

  • A statement is a source code instruction describing a type or telling program to perform an action. A simple statement is always terminated by a semi colon.

    • Console.WriteLine(" Hello.");

Well.. We can start off like this. Let’s see more in the next blog..

Wednesday, April 30, 2008

Some basics of Microsoft .NET - Part 02

Features of .Net explained…

· Object Oriented Development Environment.
· Automatic Garbage collection
· Interoperability
· No COM Required
· Simplified Deployment
· Type Safety

Object Oriented Development Environment.


Basic Components of .Net ( CLR, BCL, and Programming Languages like C# or VB) all are perfectly designed to work in a well integrated Object oriented environment. One of the keen feature of .Net framework is its consistent Object oriented programming model.

What surprised me is that, it even provide a wide range of platform target. You can write a web application or windows application which can work from high end servers to a Cell phone.
Who else will give such a broad range ?? :)

Automatic Garbage collection

If you are C++ or C programmer you will feel relief now… CLR has a tool called Garbage Collector which automatically manages memory. Well its like a memory watcher but does more.

Garbage collector deletes all objects from memory which is no longer accessed in the program. ( means less effort in memory management of your application)

As I told earlier, if you are traditional programmer, you will be hunting for all kinds of memory leaks and will be reallocating those memories. Well this is not a small feature, believe me it’s a time taking task to find out the memory leak issues… In these cases Garbage collector is a great pain reliever .. :D


Interoperability


Interoperability, I guess this will be one among the most good feature that attracted many programmers. .Net Framework is designed to work with different .Net languages , different OS and different COM components.

Recently I read a whitepaper from Microsoft, it says .Net framework will soon support programming languages like Python and Ruby. Means it will become DLR ( dynamic Language Runtime environment).

.Net even allows to inherit from a class written in another . Net language ( as long as certain rules are followed ).

Platform invoke feature ( managed code) allows code written in .Net to call and use code which were not written for .Net , such as win32 system calls.

Are you a MFC coder ? or does your current programs uses lot of COM components ?
No worries… .Net framework allows interoperability with COM. Means .Net components can call a COM component and COM component can call .NET components.


No COM Required
.Net framework will free you from the bondage of COM legacy. Since you will not need any of the following as in COM programming ( following are familiar for COM programmers… others just pass on J )

o IUnknown interface :
§ In COM all objects must implement the interface IUnknown.
§ All .Net objects are derived from a single class known as Object. Interface programming is never a central theme any more.
o Reference Counting :
§ Programmer no longer has to keep count of references to objects. Garbage collector keeps track of all references and it removes the inappropriate objects.

o HRESULT :
§ Atleast some of you might have seen this word. It shows up when errors occur in COM. HRESULT data type is used for returning runtime error codes.
§ In .Net its no longer used, instead all runtime errors produces exceptions. And of course, we can handle all these exceptions in the code.

o Registry settings:
§ If you have developed any application and deployed using any other .Net languages, you knows the headache that comes with registry.
§ .Net simplifies the deployment ( both installation and removal) of programs , not using operating system configurations or application configurations in the registry.
Simplified Deployment


Oops.. I have to tell the same thing here.. “.Net simplifies deployment because it seldom uses registry settings. ”
One good advantage is the side-by-side execution of different versions of a DLL. Earlier DLLs were used based on their names. But .Net applications uses only that DLL for which it was built.
Type Safety


Its CLR again.. CLR checks for type safety of parameters and data objects used in .NET


An overview:
Well, I will give you an overview of Compilation and Execution process in .Net.



Compilation and Execution process is same regardless of the Language of the original Source file.

Hmmm… two type of compilations.. CIL and Native code compilations… will explain more later…

Monday, April 28, 2008

Some basics of Microsoft .NET - Part 1

Some basics of Microsoft .NET
Microsoft.net is out since 2002… may be too late to mention basics now… but still it interesting to have an overview of what we learned.

.Net Framework is a much more consistent and object-oriented environment than either the MFC or COM programming technologies. In short we can say it has three basic features..
· Multiple platform: .Net Framework runs on a broad range of computers, from servers and desktop machines to PDAs and cell phones.
· Industry standards: The system uses industry standard communication protocols, such as XML, HTTP, SOAP, and WSDL.
· Security: The system can provide a much safer execution environment, even in the presence of code obtained from suspect sources.

.Net Framework mainly have three main components.


Main execution environment is known as CLR ( common Runtime Environment). CLR manages program execution at run time, and also it includes the following.
1. Memory management
2. Code safety verification
3. Garbage Collection
4. And Finally code execution

Programming tools is nothing but the tool used for making programs. It will be Visual Studio IDE for most cases. These tools also include debuggers, compliers etc.

The Base Class Library ( BCL) is a large class of Library used by .NET framework and which is used in .Net programming.

What are the benefits of using .NET Framework ?
Yes , since you have lot of platforms available that question is natural. .Net Framework is definitely an improved Programming environment.

Following are its features:
Object Oriented Development Environment.
Automatic Garbage collection
Interoperability
No COM Required
Simplified Deployment
Type Safety


Hmmm..
That’s for now… time is limiting to write more.. will update this blog next day..

Sunday, April 20, 2008

ASP.NET 3.5 - Anatomy ( for beginners)

ASP.Net 3.5
ASP.NET 3.5 is the next milestone in the evolution of Microsoft's ASP.NEt Platform. This time, this verison features AJAX, integration with the windows communication Foundation ( WCF ) service platform, Language INtegrated Query ( LINQ ) support and a few new server controls to fill existing functional gaps...

For Beginners:
What is ASP.NET?
Before asp.net, there were three main technologies and platforms for web development.
  1. ASP
  2. JSP
  3. LAMP ( open source web platform : Linux plus Apache plus MySQL plus either Perl, Python or PHP as the programming language)

Although each has a language-specific or platform specific features, all these were web development platforms designed to create interactive web pages rather than the lifeless simple HTML pages.

Like other web development environment ASP.NET also works on the top of HTTP protocol. But what differentiate it from other web development technologies is its abstract programming models.... ( the web forms model )

In addition the whole ASP.NET platform comes as part of Microsoft .NET Framework.
In short ASP.NET applications are compiled pieces of code, which can be written in first class languages ( like C#.NET, VB.NET, Jscript.NET, J# ) and which can access the entire classes in the .NET framework.


what is latest to come in future... ????
  • I have seen some whitepapers says ASP.NET will become a DLR ( dynamic Language runtime environment) when it supports programming languages like Python and Ruby. So ASP.NET is having a bright future... :)

Well as a programmer you need to know, more about the File extensions of ASP.NET applications.
  • .aspx => Files that represent ASP.NET Pages
  • .asmx => Files that implement .NET web services
  • .ascx => ASP.NET user control files.
  • .axd => file extension used for application level tracing ( trace.axd) or script level injection ( webresource.axd)
  • .ashx => HTTP handlers ( managed modules ) that interact with low level request and response services of IIS
  • .asax => ASP.NET application file such as Global.asax. This file cannot be requested directly.

In addition to this, .cs , .csproj, .vb, .vbproj, .config and .resx ( the typical Microsoft Visual Studio files) are also handled by ASP.NET.


Next time we will see how this files are requested and who handles those requests....

Post your comments and suggestions too... ;)