Playing with IL code

July 31, 2007

This is an interesting post on how to tweak the IL code. And here is an example on how to modify the IL code for a speacial case of tail recursion, to remove the stack overflow error.


Visual Studio profiler : Difference between Elapsed and Application Time

July 19, 2007

I had been struggling to understand what does the  terms mean in the VS profiler. The following two posts helped me to understand

searching text in file using boyer moore algo

July 17, 2007

While looking out for algorithms to search text in a file. I found Boyer-Moore algo to be fastest one, you can get the c# code for the algorithm from


July 11, 2007

Epsilon ε is the fifth letter of Greek alphabet , in calculas an arbitary small positive quantity is denoted as ε.

In dot net Double.epsilon field represents the smallest positive Double value greater than zero.

The field is constant and according to MSDN its value is 4.94065645841247e-324.

Defining variable

July 5, 2007

Can you declare variables on demand? I mean to say whenever in my code, I need a variable- I declare the variable, will this improve the performance of my application?

The answer is no, the CLR defines all the variables of a function at the starting of the funtion,  so no performance benifits etc., this can be checked by reading the IL of the code using ILDASM.

more command

June 24, 2007

I have created the following code snippet to print the Application.cfm file content, paginated on the system console (similar to the more command)

TextReader tr = new StreamReader(@”c:\Application.cfm”);
while (tr.Peek() != -1)
if (System.Console.CursorTop >= System.Console.WindowHeight – 4)
System.Console.WriteLine(“Press any key to continue…”);

chap 1

June 24, 2007

I have finished reading the first chapter, and following are my notes

  • using directive directly inside the namespace is invalid
  • using directive statements are not commulative
  • a class not nested in any namespace is in the global namespace
  • namespaces can be nested
  • Main is entry point a C# program and Main can be in a class or a struct
  • Main method can be private
  • Main method cannot be overloaded
  • If there are multiple classes with Main method then you can set startup in the project properties
  • the default exit code for an application is zero,The exit code of a process is stored in the Process Environment Block (PEB) and readable through the GetExitCodeProcess API.
  • ? to declare a Nullable <T> type
  • Read here to understand why Nullable type struct was introduced
  • ?? to check for null value
  • C# switch does not allow cascading between switch case statements, it is only allowed if case does not have any statements defined
  • switch(k) where k can be int, char, enum or string type or any object, value or ref which can be converted to these types
  • The foreach statement confirms that the enumerator or elements of the collection are disposable objects. If so, the foreach statement calls the Dispose method on the applicable objects. Disposable objects implement the IDisposable interface.

Learning C#

June 24, 2007

I have picked the book “Programming Visual c# 2005” to be read from cover to cover.


June 24, 2007

What is boxing and unboxing?

To convert a value type to reference type


Best practice:
int i = 10;
Console.Writeline(“i = ” + i + ” ” + i);
Writeline invokes String Concat(object arg1, object arg2) method to generate the string to be passed on to Writeline method.
Now here when passing the value of i boxing happens. If there is a reptead use of a variable in the Writeline statement it can explicitly boxed and then passed on to the Writeline function.

Which is faster?
int i =10;
System.Int32 i =10;
Ans:Both will generate the same IL code

generic class

June 24, 2007

Will the following code compile ?

class testgeneric<type>
type _i;

public type I
get { return _i; }
set { _i = value; }

class testgeneric
int _i;

public int I
get { return _i; }
set { _i = value; }


Ans: Yes