title: How to replicate a global variable in C#?
body: <p>I have a variable in one loop in C# that cannot be recognized in the other one, and I am aware that it is not possible to create a true global variable in C#, however I wonder if one can mimic one. Some of my code is this:</p>
  
  <pre><code>foreach (string line in lines)
  {
      if (line.Contains("write"))
      {
          var tempctr = line.Replace("(", "");
          var tempctr2 = line.Replace(")", "");
          var ctr = tempctr2.Remove(0, 6);
          Console.Write(ctr);
      }
      else if (line.Contains("sayinput"))
      {
          Console.Write(usrinput);
      }
      else if (line.Contains("inputget"))
      {
          var tempctr = line.Replace("(", "");
          var tempctr2 = line.Replace(")", "");
          var ctr = tempctr2.Remove(0, 9);
          Console.Write(ctr);
          string usrinput = Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>The code reads from a text file and runs a certain command based on what is in the text. My intention is for it to create a variable with <strong>inputget</strong> and spit it back out with <strong>sayinput</strong>.
  And the first <em>usrinput</em> reference is an error, since the variable is declared outside of the loop.</p>