ref 与 out 参数的区别

Ref Out的区别在于,数组类型的 ref 参数必须由调用方明确赋值,使用数组类型的 out 参数前必须先为其赋值

 

下边是分别用ref Out写的两个例子

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplication12

{

     class Program

     {

         static void Main(string[] args)

         {

              int[] myArray =new int [5]{1,2,3,4,5};

               FillArray(ref  myArray);

            for (int i=0; i < myArray.Length; i++)      

             Console.WriteLine(myArray[i]);

         }

        

         static public void FillArray(ref  int[] myArray)

         {

              // Initialize the array:

              myArray[0] = 123;

         }

 

     }

 

}

 

 

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplication12

{

     class Program

     {

         static void Main(string[] args)

         {

              int[] myArray; // Initialization is not required

            FillArray(out myArray);

           for (int i=0; i < myArray.Length; i++) 

             Console.WriteLine(myArray[i]);

         }

        

          static public void FillArray(out int[] myArray)

         {

              // Initialize the array:

              myArray = new int[5] { 1, 2, 3, 4, 5 };

         }

 

     }

 

 

}

posted @ 2008-12-15 18:37  日落无语  阅读(161)  评论(0编辑  收藏  举报