c# - Need Explaination: plug in method with delegates -


i'm new @ c# , i've been studying month now. , code giving me headache. please explain me how works, how can void method in util class change int array in test class? (http://msdn.microsoft.com/en-us/library/orm-9780596527570-03-04.aspx)

i don't understand how line values[i] = t(values[1]) can change mainvalues[] eventhough it's void without ref or out? if it's possible please explain c# beginner.

thanks time!

public delegate int transformer (int x);  public class util {   public static void transform (int[] values,transformer t)   {    (int = 0; < values.length; i++)    values[i] = t(values[i]);   } }  class test {   static void main(  )   {     int[] mainvalues = new int[] {1, 2, 3};     util.transform(mainvalues, square);      // dynamically hook in square     foreach (int in mainvalues)       console.write (i + "  ");           // 1   4   9   }    static int square (int x) { return x * x; } } 

because arrays reference types.even if don't use ref keyword passed reference.you changing values of array inside of method.you passing each value square method , getting the result replace each number new result.

values[i] int, passed value , t(values[i]) returns new value (doesn't change value have).the important part assigning back.that's why changes original value.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -