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();
        }
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM