1.28. C # passes an array to the function

发布时间 : 2025-10-25 13:34:34 UTC      

Page Views: 10 views

In C #, you can pass an array as an argument to a function. You can pass a pointer to the array to the function by specifying the name of the array without an index.

1.28.1. Example #

The following example shows how to pass an array to a function:

Example #

using System;
namespace ArrayApplication
{
   class MyArray
   {
      double getAverage(int[] arr, int size)
      {
         int i;
         double avg;
         int sum = 0;
         for (i = 0; i < size; ++i)
         {
            sum += arr[i];
         }
         avg = (double)sum / size;
         return avg;
      }
      static void Main(string[] args)
      {
         MyArray app = new MyArray();
         /* An int array with 5 elements */
         int [] balance = new int[]{1000, 2, 3, 17, 50};
         double avg;
         /* Passing pointers to arrays as parameters */
         avg = app.getAverage(balance, 5 ) ;
         /* Output return value */
         Console.WriteLine( "The average value is: {0} ", avg );
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

The average value is: 214.4
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。