1.29. C # parameter array

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

Page Views: 9 views

Sometimes, when you declare a method, you cannot determine the number of parameters to pass to the function as arguments. The C # parameter array solves this problem, which is usually used to pass an unknown number of arguments to the function.

1.29.1. Params keyword #

When using arrays as formal parameters, C # provides params keyword, which allows you to pass either an array argument or an array of elements when calling a method with an array of formal parameters. params format of the use is:

Public return type method name (params type name [] array name) 

1.29.2. Example #

The following example shows how to use a parameter array:

Example #

using System; namespace ArrayApplication { class ParamArray { public int AddElements(params int[] arr) { int sum = 0; foreach (int i in arr) { sum += i; } return sum; } } class TestClass { static void Main(string[] args) { ParamArray app = new ParamArray(); int sum = app.AddElements(512, 720, 250, 567, 889); Console.WriteLine("The total is: {0}", sum); Console.ReadKey(); } } } 

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

The total is: 2938 
《地理信息系统原理、技术与方法》  97

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