Abstract
在C/C++,array傳進function有很多技巧,但在C#,卻非常的單純,因為array自帶GetLength(),本篇主要是針對C/C++做比較。
Introduction
1
/**/
/*
2
(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4
Filename : ArrayPassToFunction.cs
5
Compiler : Visual Studio 2005 / C# 2.0
6
Description : Demo how to pass array to function
7
Release : 02/09/2007 1.0
8
*/
9
using
System;
10
11
class
Foo
{
12
static void func(int[] arr)
{
13
for(int i = 0; i != arr.GetLength(0); ++i)
{
14
Console.WriteLine(arr[i]);
15
}
16
}
17
18
public static void Main()
{
19
int[] ia = new int[]
{0, 1, 2};
20
func(ia);
21
}
22
}


2

3

4

5

6

7

8

9

10

11



12



13



14

15

16

17

18



19



20

21

22

13行,因為array自帶GetLength(),所以一切變得很單純,而且在C#,array是reference type,也沒什麼pointer的問題,就當成pass by value的寫法就好了。
See Also
(原創) array傳進function該怎麼寫才好? (C/C++)
(原創) 如何使用function template傳遞array? (C/C++) (template)