c# - Passing arrays as parameter -


if modify content of array passed parameter inside method modification done on copy of argument instead of original argument hence result not visible.

what process happens when call method have reference type argument?

here code sample of want ask

      using system;  namespace value_refrence_type {     class program     {         public static void main()         {             int[] callingarray = { 22, 200, 25485 };             abc(callingarray);             console.writeline("this callingarray");             foreach (int element in callingarray)                 console.writeline(element);         }            //method parameter         static void abc(int[] calledarray)         {             console.writeline("method called--------");             foreach (int element in calledarray)                 console.writeline(element);              //here on changing value of elements of calledarray does't afftect value of element of callingarray             //if both refrences same memory location value needs change, not happening here             calledarray = new int[] {55, 54, 65};             foreach (int element in calledarray)                 console.writeline(element);         }      } } 

no, not correct.

arguments passed value default in c#, means copy of variable. it's important realize copied variable, not object; if variable holds reference type (an array example) variable "pointer" memory address object lives. when pass said variable method call, reference copied yes, still points exact same object original variable refers to.

things different when argument value type. in case variable holds object , therefore you'd behavior seem expecting.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -