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