c# - Why reversing a string array inside method does not persist -
c# - Why reversing a string array inside method does not persist -
so i've got simple string array. passed function reverses input , displayed content of array. expecting contents of array reverse since arrays passed reference, string array did not change.
string[] words = { "metal", "gear", "is", "awesome!" }; mutatearray(ref words); foreach (string word in words) console.write(word + " "); this mutatearray function:
public static void mutatearray(ref string[] arr) { arr = arr.reverse().toarray(); } i know mutatearray method changes array persist 1 time state parameter must passed in keyword ref.
classes, interfaces, array, delegates) value vs passing them reference (with keyword ref)?
all parameters passed value default in c#. reference types array, means reference passed value.
ref causes variable passed reference function. means arr parameter in mutatearray alias words in caller. why assignment arr results in alter in words after mutatearray has exited.
passing reference type value function means re-create of reference made. without ref modifier, arr in mutatearray different variable containing reference same object words in caller. assigning arr in case has no effect on words in caller. note can mutate array through shared reference, arr , words separate storage locations.
c# parameters
Comments
Post a Comment