How to find method execution time in c#

In some time i wanted to measure method or the loop execution time in c#. I found that using c# Stopwatch class we can measure it. Here i have provided a simple example. Measuring code execution time can be used to optimize the code.

Here is the utility class which contain methods of calculating.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Elapsed_Time
{
    /// <summary>
    /// Source URL: http://stackoverflow.com/questions/2329079/how-do-you-convert-stopwatch-ticks-to-nanoseconds-milliseconds-and-seconds
    /// </summary>
    public static class Utility
    {
        public static long ElapsedNanoSeconds(this Stopwatch watch)
        {
            return watch.ElapsedTicks * 1000000000 / Stopwatch.Frequency;
        }
        public static long ElapsedMicroSeconds(this Stopwatch watch)
        {
            return watch.ElapsedTicks * 1000000 / Stopwatch.Frequency;
        }
        public static long ElapsedMiliseconds(this Stopwatch watch)
        {
            return watch.Elapsed.Milliseconds;
        }
        public static long ElapsedSeconds(this Stopwatch watch)
        {
            return watch.Elapsed.Seconds;
        }
        public static long ElapsedMinutes(this Stopwatch watch)
        {
            return watch.Elapsed.Minutes;
        }
        public static long ElapsedHours(this Stopwatch watch)
        {
            return watch.Elapsed.Hours;
        }
    }
}

and i have added another simple class which contain my looping methods and this class looks like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Elapsed_Time
{
    public class Common
    {
        public void runWithForLoop(List<int> values)
        {
            for (int i = 0; i < values.Capacity; i++)
            {

            }
        }
        public void runWithForEachLoop(List<int> values)
        {
            foreach (int i in values)
            {

            }
        }
    }
}

and my main class is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace Elapsed_Time
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch objStopWatch = new Stopwatch();
            Common c = new Common();
            List<int> values = new List<int>();

            for (int i = 0; i < 1000; i++)
            {
                values.Add(i);
            }

            objStopWatch.Start();
            c.runWithForLoop(values);
            objStopWatch.Stop();
            Console.WriteLine("Time elapsed for loop \n{0}\tHours \n{1}\tMinutes \n{2}\tSeconds \n{3}\tMiliseconds \n{4}\tMicroseconds \n{5}\tNanoseconds", Utility.ElapsedHours(objStopWatch), Utility.ElapsedMinutes(objStopWatch), Utility.ElapsedSeconds(objStopWatch), Utility.ElapsedMiliseconds(objStopWatch), Utility.ElapsedMicroSeconds(objStopWatch), Utility.ElapsedNanoSeconds(objStopWatch));

            objStopWatch.Restart();
            Console.WriteLine();
            objStopWatch.Start();
            c.runWithForEachLoop(values);
            objStopWatch.Stop();

            Console.WriteLine("Time elapsed foreach loop \n{0}\tHours \n{1}\tMinutes \n{2}\tSeconds \n{3}\tMiliseconds \n{4}\tMicroseconds \n{5}\tNanoseconds", Utility.ElapsedHours(objStopWatch), Utility.ElapsedMinutes(objStopWatch), Utility.ElapsedSeconds(objStopWatch), Utility.ElapsedMiliseconds(objStopWatch), Utility.ElapsedMicroSeconds(objStopWatch), Utility.ElapsedNanoSeconds(objStopWatch));

        }

    }
}

and the final output is

method_execution_time_32

how to set focus on textbox after post back in asp.net

I had this issue when i manually Post Back our application while searching and filtering a data in a Grid View  So i searched on the internet and finally i completed it with combination of several examples. These codes did not invented by me. But i combined these examples into one working example and all credits should goes to original developers or bloggers and stackoverflow website. This is how i Post Back the page while typing in a text box in asp.net with c# in our application.

asp.net code similar to below.

<asp:TextBox runat="server" ID="txtAutoSearch" AutoPostBack="True" 
  ontextchanged="txtAutoSearch_TextChanged" CssClass="content"
  ClientIDMode="Static" OnKeyUp="onTextBoxChanging(this);">
</asp:TextBox>

and the onTextBoxChanging(this) method similar to

<script type="text/javascript">

    var tbPostBackTimeout = null;

    function onTextBoxChanging(sender) {
        clearTimeout(tbPostBackTimeout);

        tbPostBackTimeout = setTimeout(function () {
          afterTimeout()
         }, 1000);
    }

    function afterTimeout() {
        __doPostBack('<%=txtAutoSearch.UniqueID %>', "onkeyup");

    }

</script>

here txtAutoSearch is the text box which cause the postback and on the asp.net c# section we can call java script function to focus end of txtAutoSearch text box.In the Page_Load section we can call this piece of code

if (Request.Params["__EVENTTARGET"] == txtAutoSearch.UniqueID && Request.Params["__EVENTARGUMENT"] == "onkeyup")
                {
                    focusToTextBox();
                }

and finally focusToTextBox() method similar to

private void focusToTextBox()
        {
            string str = "var input = $('#txtAutoSearch');";
            str += "var len = input.val().length;";
            str += "input[0].focus();";
            str += "input[0].setSelectionRange(len, len);";

            ScriptManager.RegisterStartupScript(Page, this.GetType(), "id", str, true);
        }