using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static void Main()
    {
       // Create new stopwatch
       Stopwatch stopwatch = new Stopwatch();

      // Begin timing
      stopwatch.Start();

      // Do something
      for (int i = 0; i < 1000; i++)
      {
        Thread.Sleep(1);
      }

      // Stop timing
      stopwatch.Stop();

      // Write result
      Console.WriteLine("Time elapsed: {0}",
          stopwatch.Elapsed);
    }
}

Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals
         static void Main(string[] args)
        {
            try
            {               
                throw new Exception();
            }
            catch (Exception ex)
            {
                // Get stack trace for the exception with source file information
                var trace = new StackTrace(ex, true);

                // Get the top stack frame
                var frame = trace.GetFrame(0);

                // Get the line number from the stack frame
                var line = frame.GetFileLineNumber();

                var LineNumber = ExceptionHelper.LineNumber(ex);

                System.Diagnostics.StackTrace trace_ = new System.Diagnostics.StackTrace(ex, true);
                var lineNo = trace_.GetFrame(0).GetFileLineNumber();

                var message = ex.Message;
                var HelpLine = ex.HelpLink;
                var Source = ex.Source;
                var StackTrace = ex.StackTrace;
                var MethodName = ex.TargetSite;              

            }

        }

 

     public static class ExceptionHelper
    {
        public static int LineNumber(this Exception e)
        {

            int linenum = 0;
            try
            {
                linenum = Convert.ToInt32(e.StackTrace.Substring(e.StackTrace.LastIndexOf(":line") + 5));
            }
            catch
            {
                //Stack trace is not available!
            }
            return linenum;
        }
    }


-----------------------------------------------------------------------------------------------------
 A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs.

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}
using System;

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

   public static Singleton Instance
   {
      get
      {
         if (instance == null)
         {
            lock (syncRoot)
            {
               if (instance == null)
                  instance = new Singleton();
            }
         }

         return instance;
      }
   }
}
         private static XDocument XMLDocumentToXDocument(XmlDocument doc)
        {
            return XDocument.Parse(doc.OuterXml);
        }
       public static string Compress(string s)
       {
           var bytes = Encoding.Unicode.GetBytes(s);
           using (var msi = new MemoryStream(bytes))
           using (var mso = new MemoryStream())
           {
               using (var gs = new GZipStream(mso, CompressionMode.Compress))
               {
                   msi.CopyTo(gs);
               }
               return Convert.ToBase64String(mso.ToArray());
           }
       }
       public static string Decompress(string s)
       {
           var bytes = Convert.FromBase64String(s);
           using (var msi = new MemoryStream(bytes))
           using (var mso = new MemoryStream())
           {
               using (var gs = new GZipStream(msi, CompressionMode.Decompress))
               {
                   gs.CopyTo(mso);
               }
               return Encoding.Unicode.GetString(mso.ToArray());
           }
       }
-------------------------------------------------------
 Other ways:-
       public static byte[] Compress(string s)
       {
           var bytes = Encoding.Unicode.GetBytes(s);
           using (var msi = new MemoryStream(bytes))
           using (var mso = new MemoryStream())
           {
               using (var gs = new GZipStream(mso, CompressionMode.Compress))
               {
                   msi.CopyTo(gs);
               }
               return mso.ToArray();
           }
       }
       public static string Decompress(byte[] s)
       {
           var bytes = (s);
           using (var msi = new MemoryStream(bytes))
           using (var mso = new MemoryStream())
           {
               using (var gs = new GZipStream(msi, CompressionMode.Decompress))
               {
                   gs.CopyTo(mso);
               }
               return Encoding.Unicode.GetString(mso.ToArray());
           }
       }

Next PostNewer Posts Previous PostOlder Posts Home