Tuesday, July 14, 2009

The difference between debug mode & release mode

Debug Mode

This is used when developer develops an application
Complete symbolic debug information is emitted
Code optimization is not taken in to account
The IsJitOptimizerEnabled property (JIT compiler uses this attribute to disable optimizations for debug mode) is True
Generates .pdb file with debug information like breakpoints and assertions

Release Mode

This is used when developer delivers an application
Complete symbolic debug information is not emitted
Code execution is optimized
The IsJitOptimizerEnabled property is False

Size of the final executable is lesser than the size of the debug executable

Note: it is a good practice to keep the release and debug code similar, and not to have different behaviour, except extra (and slow) assertion

Monday, June 22, 2009

.NET non-generic collections @ a glance!

Array – IEnumerable, System.Array

Array size is fixed
Can be single dimensional or multidimensional or jagged
All items in the array are of same type (Strongly typed)
Indexed collection

For one-dimensional arrays compilers use built in MSIL code which leads quick and effective processing.
This is the most effective way to keep fixed sized, index based collections.
Value types in an array are stored contiguously in managed heap in its unboxed form.
Reading from and writing to array is very fast because it store data in managed heap contiguously.

Index-based check comes at a slight cost of performance for applications that make a large number of array accesses

ArrayList – IList, System.Collections

Array size is dynamically increased.
Not sorted
Accessed using integer index
Weakly typed collection of objects
Can contain any type of data (as opposed to Arrays)

If the performance does not matter a lot use ArrayList.
Ideal for directly accessing and storing data items
Flexible – so far, no type or size limitations (as opposed to Arrays)

If you only want to handle a collection of specific type it is better to use an array.
Since ArrayList stores items of object type, when you need to read value you have to cast it explicitly.
When storing value types in ArrayList they are boxed and kept as object references in managed heap which degrades performance.

Queue – ICollection, System.Collections

First In First Out collection of objects (FIFO)
Objects are inserted in one end and returned/accesses in the other end
Capacity is increased due to the requirement (ref: growth factor)
Stores heterogeneous elements
Maintains internal circular array
Default capacity – 32 items

Useful for data that requires sequential processing (one element at a time in order)

Limitations on the way data is being accessed (does not allow random access)
Object needs to be cast to its own type instance before use.

Stack – ICollection, System.Collections

Last In First Out collection of objects (LIFO)
Implemented as a circular buffer
Capacity is increased due to the requirement
Count is less than the capacity of the stack, Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count. Pop is an O (1) operation
Default capacity – 10 items

Computer applications – call stack, parsing

For both Stack and Queue the time needed to add or remove an item is a constant and independent of the number of the items. (Conditions apply!)

SortedList – IDictionary, System.Collections

A collection of key-value pairs which is sorted by keys
Data is accessed by key or index
the index of a specific key/value pair might change as elements are added or removed from the SortedList object
You can access key-value pair using DictionaryEntry object
A hybrid of HashTable and array

If you want your data to be stored in a sorted manner, use this collection.

Usually operations related with SortedList are slower than that of HashTable, yet flexible.

HashTable – IDictionary, System.Collections

Represents a collection of key-value pairs based on the hash code of the key
Each element in the key-value pair can be accessed using a DictionaryEntry object
Both key and the value can be of any type

This should be used when the number of items in the collection is large.
Searching is relatively very fast
All the elements are stored in a contiguous order
Size is limited only by the available disk storage

To be continued...

Thursday, June 18, 2009

Advantages of…

Advantages of Interfaces

Makes the code cleaner and reusable
Functionality is defined but not
implemented; so definition is separated by implementation.
You can define a
single implementation which can define multiple interfaces
(Interfaces of C#
are provided as a replacement of multiple inheritance)
Interfaces are better
in situations in which you do not need to inherit implementation from a base
Structures cannot inherit classes but they can implement
(So, this is useful when you can’t use inheritance)
Easy to develop by a team
Interfaces are a logical way of grouping
objects according to the functionality/behavior
Enhances abstraction

Advantages of Inheritance

Code reusability
Derived classes and base class can be used
Derived classes can extend base class’s functionality with
specific methods.
(Base class should leave the specific method implementation
to derived classes so that any kind of enhancement/extension is
Code can move from general purpose classes to very specific classes
by adding additional functionality at the each level.
Ability to make changes
to the base class that will appear in all derived classes
Data hiding (Base
class can keep some members private so that they cannot be altered by derived

Advantages of partial classes

You can split the class definition to multiple source files.
It separates the
designer generated code (in UI) from business logic.
Team can develop the
code as separate blocks. (Multiple developers can work simultaneously)
to differentiate the automatically generated code and user implemented
All the sections of the code will combine at the compile
Improves code readability

Advantages of generics in .NET 2.0

Reduce runtime errors (If there is any error in type casting it will be detected
at the compile time rather than throwing an exception in the
Improves performance – Generics do not require boxing and unboxing
as opposed to ‘object’ type.
Type safety
Avoid code duplication
You can
use constraints in case you want to go beyond the capabilities of the Object

Advantages of delegates

Type safety
Encapsulates a method with a specific signature
Provides an
object-oriented, more secure functionality of a function pointers in
Allow decoupling between the event sender and event receiver.
When an
event occurs we can call one or more functions simultaneously

Wednesday, June 17, 2009

Stack VS Heap

Why structures are more efficient than classes?

Structures are value types and classes are reference types. Value types store data in the stack where as reference types store data in the heap (They store a pointer to that data in stack)


It works in LIFO order.
Static memory allocation
Each thread gets a stack
The size of stack is decided when the thread is created (It is limited)
Stack is faster as the way data is stored is simple and it is easy to access data in a sequence.
Each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor's cache, making it very fast.
Stack is better if you know how much data you are going to store at the runtime and if it is a small amount. (Too much data will cause stack overflow)
Variables created in stack can go out of scope and memory is de allocated.
The best thing about stack is you don’t have to worry about garbage collection.
Stack is more expensive, yet faster.


Dynamic memory allocation.
There is no exact patter or order in allocating memory. It is arbitrary.
There’s only one heap for the application.
The size of heap is decided in application start up but can enhance in need.
Heap has more complex way of allocating and accessing memory.
Variables in heap should be removed manually as they do not go out of scope.
When you use the “new” keyword the memory is allocated in heap.
The size of memory allocated in heap is not known to the compiler so that programmer has to assign and release it manually. (Now some languages provide this functionality as well still this is considered as a performance issue)

Tuesday, June 9, 2009

Bin folder & Obj folder

In Visual Studio projects you will find two different folders with almost the same files inside. Earlier, I was confused with these two folders. But now I have found the difference between the bin folder and obj folder.


Bin file contains compiled assemblies (*.dll files) of the application which contains MSIL code and some Meta data. Bin is where the final compiled code is stored. You should use this code to deploy/run your application.

Assemblies in the Bin folder do not need to be installed in the Global Assembly Cache (GAC). The presence of a *.dll file in the Bin folder is sufficient for ASP.NET to recognize it. If you change the *.dll and write a new version of it to the Bin folder, ASP.NET detects the update and uses the new version of the .dll for new page requests from then on.


Intermediate files are stored in the obj folder. When you build your application once it keeps the compiled version of that code and if you rebuild it uses that existing compiled version to link ,which improves efficiency.

This consist of *.dll, *.pdb and *.csproj.FileListAbsolute files.

*.csproj.FileListAbsolute file is a text file which contains a list of related compiled versions of the project.
*.pdb (Program Debug Database) file holds debugging information. (Generate Debug Info option is specified by default. That’s why the linker generates this file.)

When you clean the solution, all the temporary files under this folder will get deleted.
(build => cleansolution)

Basically, you don’t have to worry about what is inside this folder because bin folder contains the final version, so that you may use it, unless you are really curious about what this is all about (like me).
You have release and debug sub folders under these directories to differentiate the builds.

Monday, June 8, 2009

Visual Studio web site VS Visual Studio web application

Although, it seems that web site and web application are similar , I should emphasize that it is not.

If you don't understand the difference between these two and start implementing your application, you will be in real trouble. (Actually I faced that situation)

Web site

You can create a new web site by file => new => website.

There, you can browse and select the place where you want to save your website. If you like you can give a name for your web site in the path itself. (just over write the exsiting name. By default it is WebSite1 in file system.

In location drop down list, you will find 3 options which are file system, http and FTP.

If you select the location as file system, you should specify a folder in your hard disk or shared folder in another computer. This option does not require you to set up IIS (Internet Information Services) to host your web site, because you can use ASP.NET development server which is a built-in feature in Visual Studio 2008. Although, if you want to deploy web site in another server, you can set up your web site later in IIS as well.
(Right click on the website icon => property pages => start options => server => use custom server)

If you have IIS installed in your machine, you can create a virtual directory by right click on websites => default website and new => virtual directory and then point that to your local path of your web site.
If you select http as the location, then your folder will be saved in C:\Inetpub\wwwroot.
There’s an option called remote site. A remote Web site is a site that uses IIS but is on another computer that you can access over a local area network.

You can select an FTP server as your location by selecting FTP as well.
From this point onwards all the information is applicable for file system websites.

After you select every thing as required, Visual Studio will create a new solution for your web site. But when you want to open your web site what you have to do is go to File => open => Web site and select your folder there. Unlike web application projects, you can find your solution file in your website folder which is in My Documents => Visual Studio 2008 => Projects.

You don’t have a project file for your web site. (*.csproj) Further, you may notice that *.aspx.designer.cs files are missing.

Another special thing is there’s no bin folder. No assembly files are created when you build your web site. They will create in the run time. (I think!)

Web application

You create a web application project by File => new => Project => ASP.NET web application. You can specify the location you want to create your web application and if you like you can create a new folder for the solution. By checking ‘add to source control’ check box you can automatically add solutions and projects to source control.
Source control is a repository for the code where many developers can share the code and work on it.

When you build your project it is compiled to a single assembly which will be placed in the bin folder. You will find all the required files such as *.csproj, *.sln, web.config in the folder which you specified the path.

So, these are some differences I found and it is up to you to decide which approach will match your requirements precisely.

VisualStudio 2008 project (*.csproj) and solution ( *.sln)

Visual Studio projects and solutions are conceptual containers to store the data items such as references,data connections, files and folders we use in development.


A solution can contain one or more projects, other files and meta data.
When you create a new project (file => new => project) Visual Studio automatically creates a solution for that. So that you can add many new project as you want (rightclick on the solution => add => new project) or add existing projects (add => existing project) depending on your requirements.
You can create folders and add non-project related items also.

So, using solution is a good way of handling your system hierarchy. You can have a nice folder architecture using them.

I developed a system using Visual Studio 2008. There, I gave my folder architecture like this:
DB (all the database related stuff), class library (data layer, business layer classes), tools, UI (web application)etc.

So, if I want to refer any of these things it is easier having it in a one solution rather than having seperate projects and folders.

I can navigate through these items using the solution explorer.


Using a project, you can logically manage, build and debug your application.
When you build your project the output will be an assemply file (*.dll or *.exe) .

Visual Studio provides different project templates such as web application, class library and windows application.
You can select the template depending on your functional requirements.

Project items can be code files, references to libraries and data connections.
Visual Studio provides several templates to the project items as well. They can be web forms, html files, XML configuration files etc.