Friday, November 30, 2007

What is Garbage Collection(GC) ?

One of the major causes of program failure today, particularly in applications that run for long time is due to manual memory management. Which leads two main problems

First one is, a programmer allocates a block of memory in a data storage area Of operating system (i.e. in Random Accesses Memory) intending to Free it latter, some time he mistakenly forget to release memory it No longer needs, this condition known as ‘Memory leak’

If this application run long enough, these leaks accumulate and the application runs out of memory, that is not big deal in a programs like ‘notepad’, that user runs for a few minutes an then shutdown. But a fatal application like web server that are supposed to run continuously for days or week this will leads accumulation of memory leaks and which leads to application fail.

Second one is programmer manually delete an object but then mistakenly or other objects try to access this memory location later. This will leads to hanging of application, and more over some times transistor that had make up the deleted object memory would still contain ‘Plausible’ values, and program continue to run with corrupted data.

These two above mentioned bugs are worse than most other application bugs because what the consequences will be and when those consequences will occur are typically unpredictable. That is making our application perform in unpredictable ways at unpredictable times. For other bugs, when we see our application misbehaving, we can just fix it

Microsoft.NET Made solution for above mentioned bugs. That is Microsoft made automatic memory management has part of .Net common language runtime (CLR), which allows it is to be used in any .Net language. That is in .Net application if CLR detects that, a Application is no longer using the memory and that it no longer needed. Then CLR release that memory. (i.e. Application does not have to explicitly free Memory that allocated). This mechanism runs automatically in background and is known as Garbage Collection

It solves the problem of manual memory management with out having to write any single line of code. You can’t forget to delete an object because the system cleans it for you when it is not required and you can’t access deleted object through an invalid reference because the object won’t be deleted as long as you hold a reference it

This Garbage Collection is like an automatic seat belt. The passengers couldn’t forget to buckle it. More over we know that automatic belt require more space and mechanism than manual one. Similarly .net application Require more system resources than ordinary application.

Really the garbage collection changes programming. we don't have all that Free What we Create overhead, so our code is smaller and clearer and easier to write. Garbage collection also makes programming even more object oriented.

Main aim of .Net is that faster development with fewer bugs That is we want programmer only think about program logic. Not any other Things like memory management or else.

NOTE: The memory used by heap variables is freed via a process garbage collection. Stack variables is automatically freed when the method in which they were created returns( ie Grabage collection is applied only for Heap Variable)

Thursday, November 29, 2007

Generics in VB.NET

Generics are very useful if you know what they are and how to use them. C++ was one of the first languages, which used some types of generics, however they were called “templates”. C# uses a similar syntax as the C++ templates, however in C# they are much easier to use. <> is used in C# to define a generic. However in VB.NET we use the keyword “Of”. In this article we will only discuss the VB.NET syntax.

What are Generics?

The term “generic” means not to be bound to any specific type regardless of whether we are referring it to any programming language or not! Just think of a “glass”, which can be filled with the following types: water, juice, soda etc. As you can see it is not bound to one type. It can be filled with any type. In the same way we can refer it to VB.NET. We could create a class called “Glass” which could store any of these types depended on the users choice.

Generics can be used to store collections, which are not bound to any data type.
Why should I use Generics?

ust imagine that you are working in a class where you would need to store different data types in a collection. Of course you could use an array, but don’t forget that you will need to create different arrays for different data types. Furthermore your code will be unnecessary longer than needed. In that case we can use generics. We could use one generic class that could handle all of data types, without requiring writing the code again and again. This will increase the performance, because the user would not need to cast the data types. In summary we can say that generics are used for the following reasons:

* Performance
* Type Safe
* Code Reuse

Performance:
How can generics help the developer to get a better performance? This is simple. The data-types are checked in the compile-timer rather than in run-time. Simply this change will improve the performance. No type casting necessary at run-time. However you should know that performance and speed are not the main aspects to use generics, but in maintainability and cleaner code.

Type Safe:
This means that whenever you add an object to the collection it is checked at compile time. That means that the data type should be safe and if you pass a wrong data-type to the collection, it will throw an error during the compile time and not in the run time. This way you can make sure to handle all errors so that they don’t appear at run time, when the client is using the application.

Code Reuse:
If you want to store different data types in a (non-generic) collection, than you won’t have any other option than declaring different collections for that. For example: You will need to declare a collection for integers, another for strings and another for double data types. When using generics you just need to set the data type as a parameter and viola you can use it with whatever type you like.

Simple Example

We have read a lot by now. It’s time to see some code. To demonstrate how simple it is to use we have chosen a simple example. Lets see how it looks like:

Our Gen Class:

Public Class Gen(Of T)
Dim dataType As T
Public Property Val() As T
Get
Return dataType
End Get
Set(ByVal value As T)
dataType = value
End Set
End Property
End Class

Our Main:

Sub Main()
' This one accepts strings
Dim mystring As New Gen(Of String)
mystring.Val = "hello world"
System.Console.WriteLine(mystring.Val)
' Change the declaration so that it accepts now an integer
Dim myInt As New Gen(Of Integer)
myInt.Val = 5
System.Console.WriteLine(myInt.Val)
Console.WriteLine("Press Enter to finish ... ")
Console.Read()
End Sub

Our Output:

hello world
5

How it works:
As you can see above we have only two main parts. The first is the class “Gen” and the second is the main function. The “Gen” class is generic. As already described in the introduction we need the “Of” keyword to declare it as generic. In C# and C++ we can use <> to create generic classes.

The “T” next right to the “Of” represents our data-type which we will pass when we create a new instance of that class. The rest of the code should be familiar to you.

The interesting part is going on in the main function where we create two instances of the “Gen” class. In the first instance we are using a String. This will allow passing string values to the class variables.

Dim mystring As New Gen(Of String)
mystring.Val = "hello world"
System.Console.WriteLine(mystring.Val)

In the second instance we are using an Integer. Here we can use integer values to pass to the class variables.

Dim myInt As New Gen(Of Integer)
myInt.Val = 5
System.Console.WriteLine(myInt.Val)

As you can see this was our first simple generic example. To get the same result without using generic, will cost you two classes. The first one would need a string and the second one could use an integer, but what if you decide to create a third data-type, lets say a double? Well then you will need to create a third class, which will again make your code longer than it already is. While using generics you only need one class for any data-type.

CLI, CLR, MSIL, JIT, .NET, C#, CLS and CTS ....


To most .NET programmers, there will be nothing new in this particular post. But, it only seems fitting that the initial post to this blog should be this simplified overview, just to make sure we're all on the same page.

The Common Language Infrastructure (CLI) is an international standard defined in ECMA-335. The CLI specifies the common mechanics for high-level, CLI-compliant programming languages. This standard is neither language specific nor platform specific.

The Common Language Runtime (CLR) is Microsoft's implementation of the CLI standard. The CLR executes a type of bytecode known as the Microsoft Intermediate Language (MSIL). At run-time, that MSIL bytecode is turned into native, executable code by the Just-In-Time compiler (JIT compiler). In theory, any CLI-compliant programming language is supported on any platform with the appropriate compiler and runtime.

The term ".NET" refers to a Microsoft product that includes the CLR, JIT compiler, source code compilers, and an entire framework of CLI-compliant components targetted for the Windows platform.

C# is a CLI-compliant programming language defined by the C# Language Specification (C#LS) known as ECMA-334. C#.NET is Microsoft's implementation of the C#LS for use with the Microsoft CLR, JIT compiler and framework components on Windows platforms. Since C#.NET is the only real implementation of the C# programming language (aside from the Mono open source project), the terms "C#" and "C#.NET" are often used interchangeably.

There are several .NET languages, including C#.NET and VB.NET. Even though both of those languages are CLI-compliant, the languages themselves have different capabilities. The Common Language Specification (CLS) is a Microsoft specification that defines the guaranteed subset of common functionality that is useable by all .NET programming languages. For example, the CLS defines the Common Type System (CTS), and the CTS does not include unsigned integers because the VB.NET programming language does not support unsigned integers.

Wednesday, November 28, 2007

Monday, November 12, 2007