Thursday, January 28, 2010

Why name JIT?

.net compiler convert source code into intermediate language during compilation. this is also called as Microsoft intermediate language(MSIL).
Its the job of the JIT compiler to take MSIL and convert it to native code at run time.


.net will not convert all MSIL code into native code at once. Because it is time and space consuming. Some part of the .net code never executes. So there is no need to keep this code in the memory. This is resource consuming.


So .net came up with an approach called JIT compilation, it converts MSIL to native code on demand at application runtime. So the name JUST IN TIME COMPILING

Wednesday, January 6, 2010

Measuring execution time in C#

How to measure a method's execution time without using timer?

Use DateTime and TimeSpan classes in .net framework.

public void sampleMethod()
{
DateTime starttime = DateTime.Now;
//
//......
//do code here
//......
DateTime endtime = DateTime.Now;
TimeSpan duration = endtime - starttime;
string time = duration.Hours + ":" + duration.Minutes + ":" + duration.Seconds + ":" + duration.Milliseconds;
}

Wednesday, December 16, 2009

How much unit test coverage you need?

Not less than 80% of coverage is advisable.

Effective test should consider not only code, but also input parameters, and application state which could also affect the behavior.

  1. test success condition
  2. test failure condition
  3. test boundary condition

http://www.codeproject.com/KB/testing/UnitTestVS2008.aspx

    Wednesday, December 2, 2009

    Sealed

    • It restricts inheritance
    • Cannot derive from Sealed type
     

      Const vs ReadOnly in .net

      Const
      • It is also called as "Compile time constant"
      • Const can be assigned at variable initialization
      • Once assigned value cannot be changed
      ReadOnly
      • "Runtime constant"
      • ReadOnly field can be assigned at variable initialization and in constructor
      • Value can be changed in constuctor

      Sample

          class ReadonlyConstant
          {
              private const int x=10;     //compile time constant
              private readonly int y=20;  //runtime constant

              public ReadonlyConstant()
              {
                  Console.WriteLine("\n***Const Vs ReadOnly***");
                  Console.WriteLine("Const x before initialization=" + x);
                  Console.WriteLine("ReadOnly y before initialization=" + y);



                  //The left-hand side of an assignment must be a variable, property or indexer
                  //x = 20;   // value cannot be changed in constructor

                  y = 110;    // value can be changed in constructor   

                  Console.WriteLine("Const x after initialization=" + x);
                  Console.WriteLine("ReadOnly y after initialization=" + y);
              }

              //public int IncrementY
              //{
              //    //A readonly field cannot be assigned to (except in a constructor or a variable initializer)
              //     set { y++; }
              //}
          }

        simple .net singleton pattern

            public sealed class dotnetSingleton
            {
                public static readonly dotnetSingleton Instance = new dotnetSingleton();
                private dotnetSingleton()
                {
                }
            }

        Reference: http://msdn.microsoft.com/en-us/library/ee817670.aspx

        Wednesday, November 25, 2009

        C# constructors

        1. Static constructor
        2. Instance constructor (non-static constructor)



        Static constructor
        • Does not take access modifiers
        • Cannot access any non static members
        • Static constructor will get called as soon as we create an instance of the class or we refer the class
        • class which implements static constructor does not has to be static.
        • static constructor calls only once.
        • it can be used for initializing objects
          eg, say there is an employee class, and we want to know how many employee object's are created. This case you can create a static integer variable and increment the count.