title: References to variables in C#?
body: <p>In C++, I can do something like this:</p>
  <pre><code>int i[10] = new int[10];
  int *p = &amp;i[5];
  </code></pre>
  <p>Then, I can always know that <code>p</code> points to the 5th element of int array <code>i</code>, regardless of <code>i</code>'s contents.</p>
  <p>Is there any way to do something similar in C#?</p>
  <p>I realize this is likely one of the ways in which C# &quot;protects&quot; us from ourselves, so I'm not looking for an exact equivalent, but rather a similar concept... that is, being able to refer to the contents of some other variable, rather than the instance of the variable itself.</p>
  <p>Here's my use case I'm thinking of.  I have an array of strings.  I would like to have another array of references to those array elements.  Something like this (obviously not valid code):</p>
  <pre><code>string[] s = new string[] { &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot; };
  stringref[] sr = new stringref[] { &amp;s[0], &amp;s[1], &amp;s[2], &amp;s[3], &amp;s[4], &amp;s[5] };
  
  Console.WriteLine(sr[1]); // == &quot;two&quot;
  s[1] = &quot;two point zero&quot;;
  Console.WriteLine(sr[1]); // == &quot;two point zero&quot;
  </code></pre>
  <p>Certainly, <code>ref</code> parameters do this, and <code>out</code> parameters allow you to write to a specific variable.  But what about non-parameters?  Can you store a <code>ref</code>?  Can you keep an array of <code>ref</code>s or a dictionary?</p>
  <p>It seems like if the ability to do it with parameters is present, there should be a way to do it without them.</p>