Use StringBuilder to reverse a string

image_pdfimage_print
   
 

using System;
using System.Text;

class MainClass {
    public static string ReverseString(string str)
    {
       if (str == null || str.Length <= 1) 
       {
            return str;
       }

       StringBuilder revStr = new StringBuilder(str.Length);


       for (int count = str.Length - 1; count > -1; count--)
       {
           revStr.Append(str[count]);
       }

       return revStr.ToString();
   }
    public static void Main() {
        Console.WriteLine(ReverseString("The quick brown fox jumped over the lazy dog."));
    }
}

    


Length and Indexer

image_pdfimage_print
   
 

using System;
using System.Text;

class MainClass
{
    public static string ReverseString(string str)
    {
       if (str == null || str.Length <= 1) 
       {
            return str;
       }

       StringBuilder revStr = new StringBuilder(str.Length);


       for (int count = str.Length - 1; count > -1; count--)
       {
           revStr.Append(str[count]);
       }

       return revStr.ToString();
   }
   public static void Main(){
       Console.WriteLine(ReverseString("Madam Im Adam"));
       Console.WriteLine(ReverseString("The quick brown fox jumped over the lazy dog."));
   }
}

    


Get Text Element Enumerator

image_pdfimage_print

   


using System;
using System.Globalization;
using System.Threading;

class Class1 {
  static void Main(string[] args) {
         TextElementEnumerator Iter;
         String MyStr, OutBuf;

         MyStr = "The Quick programmer ran rings around the lazy manager";

         //Lets do the iterator thing
         Iter = StringInfo.GetTextElementEnumerator(MyStr);
         while (Iter.MoveNext())
         {
            OutBuf = "Character at position " + 
                     Iter.ElementIndex.ToString() + 
                     " = " + Iter.Current;
            Console.WriteLine(OutBuf);
         }
   }
}

           
          


Use Stopwatch

image_pdfimage_print
   


using System;
using System.Reflection;
using System.Diagnostics;

class OnlyLoad {
    static void Main() {
        Stopwatch duration = new Stopwatch();
        duration.Reset();
        duration.Start();
        Assembly a = Assembly.Load("library");
        duration.Stop();
        Console.WriteLine(duration.ElapsedTicks.ToString());
        duration.Reset();
        duration.Start();
        a = Assembly.ReflectionOnlyLoad("library");
        duration.Stop();
        Console.WriteLine(duration.ElapsedTicks.ToString());
    }
}