c# - Get value from table as a reference -


in c++ it`s possible reference (&) or pointer (*). in c# there "ref". how value table , change reference it?

namespace rextester {     public class program     {         public static void main(string[] args)         {             int[] t=new int[3]{1,2,3};             int a=t[0]; //ref int a=t[0];             a+=10;             system.console.writeline("a={0}", a);   //11             system.console.writeline("t[0]={0}", t[0]); //1         }     } } 

e.g. in c++

int &a=tab[0]; 

this has become feasible in c# 7, using ref locals:

public class program {     public static void main(string[] args)     {         int[] t = {1, 2, 3};         ref int = ref t[0];         += 10;         system.console.writeline($"a={a}");       // 11         system.console.writeline($"t[0]={t[0]}"); // 11     } } 

this important line:

ref int = ref t[0]; 

c# 7 supports ref returns. advise using both features sparingly - while can useful, unfamiliar many c# developers, , can see them causing significant confusion.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -