c#的参数调用

c#的参数传递有三种方式:
值传递,和c一样,
引用传递,类似与c++,但形式不一样
输出参数,这种方式可以返回多个值,这种有点像c中的指针传递,但其实不太一样。
值传递不细说,c中已经很详细了
引用传递实例如下:需要使用ref关键字

using System;
namespace CalculatorApplication
{
    class NumberManipulator
    {
        //引用传递必须使用ref
        public void swap(ref int x, ref int y)
        {
            int temp;

            temp = x; /* 保存 x 的值 */
            x = y;    /* 把 y 赋值给 x */
            y = temp; /* 把 temp 赋值给 y */
        }

        static void Main(string[] args)
        {
            NumberManipulator n = new NumberManipulator();
            /* 局部变量定义 */
            int a = 100;
            int b = 200;

            Console.WriteLine("在交换之前,a 的值: {0}", a);
            Console.WriteLine("在交换之前,b 的值: {0}", b);

            /* 调用函数来交换值 */
            n.swap(ref a, ref b);

            Console.WriteLine("在交换之后,a 的值: {0}", a);
            Console.WriteLine("在交换之后,b 的值: {0}", b);

            Console.ReadLine();

        }
    }
}

按输出传递参数
return 语句可用于只从函数中返回一个值。但是,可以使用 输出参数 来从函数中返回两个值。输出参数会把方法输出的数据赋给自己,其他方面与引用参数相似。
使用关键字out。
下面的实例演示了这点:

using System;

namespace CalculatorApplication
{
   class NumberManipulator
   {
		//使用out可以吧x的值重新复制给x,相当于更新
      public void getValue(out int x )
      {
         int temp = 5;
         x = temp;
      }
   
      static void Main(string[] args)
      {
         NumberManipulator n = new NumberManipulator();
         /* 局部变量定义 */
         int a = 100;
         
         Console.WriteLine("在方法调用之前,a 的值: {0}", a);
         
         /* 调用函数来获取值 */
         n.getValue(out a);

         Console.WriteLine("在方法调用之后,a 的值: {0}", a);
         Console.ReadLine();

      }
   }
}

  有out之后,a的值就变成了5。

两个参数的实例:

using System;

namespace CalculatorApplication
{
    class NumberManipulator
    {
        //使用out可以吧x的值重新复制给x,相当于更新
        public void getValue(out int x, out int y)
        {
            int temp = 5;
            x = temp;

            y = x;
        }

        static void Main(string[] args)
        {
            NumberManipulator n = new NumberManipulator();
            /* 局部变量定义 */
            int a = 100;
            int b = 20;

            Console.WriteLine("在方法调用之前,a 的值: {0}, b的值:{1}", a, b);

            /* 调用函数来获取值 */
            n.getValue(out a, out b);

            Console.WriteLine("在方法调用之前,a 的值: {0}, b的值:{1}", a, b);
            Console.ReadLine();

        }
    }
}

  再说一点就是,vs的ide功能,的确是很强大的,很多语法提示都有。

下面还有一个用处:

提供给输出参数的变量不需要赋值。当需要从一个参数没有指定初始值的方法中返回值时,输出参数特别有用。请看下面的实例,来理解这一点:
using System;

namespace CalculatorApplication
{
    class NumberManipulator
    {
        //提供给输出参数的变量不需要赋值。当需要从一个参数没有指定初始值的方法中返回值时,输出参数特别有用。请看下面的实例,来理解这一点:
        public void getValues(out int x, out int y)
        {
            Console.WriteLine("请输入第一个值: ");
            x = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第二个值: ");
            y = Convert.ToInt32(Console.ReadLine());
        }

        static void Main(string[] args)
        {
            NumberManipulator n = new NumberManipulator();
            /* 局部变量定义 */
            int a, b;

            /* 调用函数来获取值 */
            n.getValues(out a, out b);

            Console.WriteLine("在方法调用之后,a 的值: {0}", a);
            Console.WriteLine("在方法调用之后,b 的值: {0}", b);
            Console.ReadLine();
        }
    }
}

  

原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/11845588.html