类的方法(通过引用来传递参数)

  class zoo//定义一个类
    {
        private int streetNumber = 123;
        private string StreetName = "Beijing Road";
        public void GetAddress(ref int number, ref string name)//注意关键字ref
        {
            number = streetNumber;
            name =StreetName;
        }
     }

  zoo myzoo = new zoo();

        int zooStreetNumber = 0;
        string zooStreetName = null;
        myzoo.GetAddress(ref  zooStreetNumber,ref zooStreetName); //通过引用传递,实际上传递的是地址。而值传递,方法使用的是变量值的拷贝。
        if (zooStreetNumber != 0)
            {  Response.Write("streetNumber has changed!<br>");
                Response.Write(zooStreetNumber);
               }

输出的结果为:
streetNumber has changed!
123

posted on 2008-04-11 23:51  阿良  阅读(679)  评论(0编辑  收藏  举报