当前位置:编程学习 > asp >>

.Net 框架程序设计

1 值传递和引用传递
值传递是传递的值的拷贝;引用传递是传递的对象本身。

测试代码:

classProgram

    {

        staticvoid Main(string[] args)

        {

            SomeValueType valueParam;

            SomeRefType refParam = new SomeRefType();

            //*****    <1>    ****

            valueParam.Name = "before excute";

            Console.WriteLine("值类型作参数,执行前:{0}", valueParam.Name);

            Test.DoSomething(valueParam);

            Console.WriteLine("值类型作参数,执行后:{0}", valueParam.Name);

            Console.WriteLine("");

            //*****    <2>    ****

            refParam.Name = "before excute";

            Console.WriteLine("引用类型作参数,执行前:{0}", refParam.Name);

            Test.DoSomething(refParam);

            Console.WriteLine("引用类型作参数,执行后:{0}", refParam.Name);

            Console.WriteLine("");

            //*****    <3>    ****

            valueParam.Name = "before excute";

            Console.WriteLine("值类型作参数,执行前:{0}", valueParam.Name);

            Test.DoOtherSomething(valueParam);

            Console.WriteLine("值类型作参数,执行后:{0}", valueParam.Name);

            Console.WriteLine("");

            //*****    <4>    ****

            refParam.Name = "before excute";

            Console.WriteLine("引用类型作参数,执行前:{0}", refParam.Name);

            Test.DoOtherSomething(refParam);

            Console.WriteLine("引用类型作参数,执行后:{0}", refParam.Name);

            Console.WriteLine("");

            //*****    <5>    ****

            valueParam.Name = "before excute";

            Console.WriteLine("值类型引用传递,执行前:{0}", valueParam.Name);

            Test.DoSomething(ref valueParam);

            Console.WriteLine("值类型引用传递,执行后:{0}", valueParam.Name);

            Console.WriteLine("");

            //*****    <6>    ****

            refParam.Name = "before excute";

            Console.WriteLine("引用类型引用传递,执行前:{0}", refParam.Name);

            Test.DoSomething(ref refParam);

            Console.WriteLine("引用类型引用传递,执行后:{0}", refParam.Name);

            Console.WriteLine("");

            //*****    <7>    ****

            valueParam.Name = "before excute";

            Console.WriteLine("值类型引用传递,执行前:{0}", valueParam.Name);

            Test.DoOtherSomething(ref valueParam);

            Console.WriteLine("值类型引用传递,执行后:{0}", valueParam.Name);

            Console.WriteLine("");

            //*****    <8>    ****

            refParam.Name = "before excute";

            Console.WriteLine("引用类型引用传递,执行前:{0}", refParam.Name);

            Test.DoOtherSomething(ref refParam);

            Console.WriteLine("引用类型引用传递,执行后:{0}", refParam.Name);

            Console.WriteLine("");

            Console.ReadKey();

        }

    } 

    classSomeRefType

    {

        publicstring Name;

    }

    structSomeValueType

    {

        publicstring Name;

    }

    classTest

    {

        publicstaticvoid DoSomething(SomeValueType v)

        {

            v.Name = "value type param";

            Console.WriteLine("值类型作参数,执行中:"+v.Name);

        }

        publicstaticvoid DoSomething(SomeRefType r)

        {

            r.Name = "ref type param";

            Console.WriteLine("引用类型

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,