专注于高品质PHP技术等信息服务于一体 [STIEMAP] [RSS]

百度提供的广告:
PHP
当前位置:首页 > 技术文档 > PHP >  > 
各语言对于 方法传数组引用还是值传递


1,PHP
<?php

class test
{
  function t1()
  {
      $arr = array(1,2,3,4,5);
      $this->t2($arr);
      var_dump($arr);
  }
  function t2($a)
  {
          $a =  null;
  }
}
$t = new test();
$t->t1();

echo "以下为普通函数方法";
  function t3()
  {
      $arr = array(1,2,3,4,5);
      t4($arr);
      var_dump($arr);
  }
  function t4($a)
  {
          $a =  null;
  }
t3();
?>
没有影响到原来的值 PHP 为传值方式
2,JAVA
import java.util.ArrayList;
import java.util.List;


public class Test {
   
    static void t1()
    {
        List l = new ArrayList();
        l.add(1);
        l.add(2);
        l.add(3);
        t2(l);
        System.out.println(l.get(0));
    }
    static void t2(List list)
    {
        list = null;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test.t1();
    }

}
没有影响到原来的值 JAVA 为传值方式
3,C#
  class Program
    {
        void t1()
        {
            List<Int32> list = new List<int>();
            list.Add(1);
            list.Add(2);
            t2(list);
            Console.WriteLine(list[0]);
        }
        void t2(List<Int32> l)
        {
            l = null;
        }
        static void Main(string[] args)
        {
        }
    }
C# 继承了C语言里面的指针,在C C++ 里面,数组就是指针。
所以,在C# 中传List Array都是按引用传递。