c# - Wrong result of print in FUNC -
i have problem code, code need print result:
56789 56789
but result is: 99999 99999
why?
class program { static int m_k = 0; static func<int>[] p=new func<int>[5]; static void f(int n,func<int>[] t) { if (n>0) { p[m_k] = () => t[m_k](); m_k++; f(n-1,t); } else { p[m_k] = () => t[m_k](); } } static void main(string[] args) { func<int>[] t = new func<int>[5]; func<int>[] s = new func<int>[5]; t[0] = () => 5; t[1] = () => 6; t[2] = () => 7; t[3] = () => 8; t[4] = () => 9; (int = 0; < 4; i++) { s[i] = () => t[i](); } (int = 0; < 4; i++) { console.writeline(s[i]()); } f(4,t); (int = 0; < 4; i++) { console.writeline(p[i]()); } console.readkey(); } }
you tring access modified cluser!!! in computer science, closure first-class function free variables bound in lexical environment.
you can fix code this:
static void f(int n,func<int>[] t) { if (n>0) { int tmp=m_k p[m_k] = () => t[tmp](); m_k++; f(n-1,t); } else { p[m_k] = () => t[m_k](); } }
and this:
for (int = 0; < 4; i++) { int j=i; s[i] = () => t[j](); }
Comments
Post a Comment