C#之不一样参数的访问属性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Practice805_2
{
    class Program
    {

        public static void Main(string[] args)
        {
            Test test = new Test();
            //按输出方式访问参数
            double param = 1;
            double out1;
            double out2;
            double out3;

            test.method(param, out out1, out out2, out out3);
            Console.WriteLine(out1);
            Console.WriteLine(out2);
            Console.WriteLine(out3);

            TestRef testR = new TestRef();
            //按值
            double param1 = 10;
            double douNotRef = testR.testNotRef(param1);
            Console.WriteLine("param1::::" + param1);
            Console.WriteLine(douNotRef);

            //按引用 
            double param2 = 10;
            double douRef = testR.testRef(ref param2);
            Console.WriteLine("param2::::" + param2);
            Console.WriteLine(douRef);

            Console.ReadLine();
        }

    }

    class TestRef
    {
        public double testNotRef(double x)
        {
            x = x * x;
            return x;
        }

        public double testRef(ref double y)
        {
            y = y * y;
            return y;
        }

    }

    class Test
    {
        public void method(double param, out double out1, out double out2, out double out3)
        {
            out1 = param /2;
            out2 = param / 3;
            out3 = param / 4;
        }
    }
}

这里面介绍了三种参数的访问属性,分别为按值,按引用,以输出方式访问参数。比较具体,比java中的值传递仍是引用传递更明确了。这里面主要是提到基本类型默认的均为按值传递,若想改成引用,可用ref。out的输出方式能够借鉴下java

相关文章
相关标签/搜索