title: Run (C#) algorithm assessment
body: <p>I have been doing programming algorithms lately, just to practice :). 
  i got this question from a site. The problem is:</p>
  
  <blockquote>
    <p>Write a function that finds the zero-based index of the longest run in a string. A run is a consecutive sequence of the same character. If there is more than one run with the same length, return the index of the first one.</p>
  </blockquote>
  
  <p>For example, IndexOfLongestRun("abbcccddddcccbba") should return 6 as the longest run is dddd and it first appears on index 6.</p>
  
  <p>So what i came up is this :</p>
  
  <pre><code>public static int IndexOfLongestRun(string str)
      {
          string retval = string.Empty;
          string firstOccurence = string.Empty;
          string maxOccurence = string.Empty;
          string val1 = string.Empty;
          string val2 = string.Empty;
          int counter = 1;
          int occurenceCounter = 1;
          int maxOccur = 0;
  
          for (int i = 0; i &lt; str.Length; i++)
          {
              val1 = str[i].ToString();
              if (i &lt; str.Length - 1)
              {
                  val2 = str[i + 1].ToString();
              }
              else
              {
                  val2 = str[i].ToString();
              }
  
              if (val1 == val2)
              {
                  firstOccurence = val1;
                  occurenceCounter = occurenceCounter + counter;
  
                  if (occurenceCounter &gt; counter &amp;&amp; occurenceCounter &gt; maxOccur)
                  {
                      maxOccur = occurenceCounter;
                      maxOccurence = firstOccurence;
                  }
                  continue;
              }
              else
              {
                  occurenceCounter = 1;
              }
          }
          return str.IndexOf(maxOccurence, 0);
      }
  </code></pre>
  
  <p>This past the me on the main purpose of the test. however it fails me on performance benchmark. Could anyone shed some light on how this code of mine can be optimized? Thanks.</p>