(原創) array傳進function該怎麼寫才好? (.NET) (C#)

Abstract
在C/C++,array傳進function有很多技巧,但在C#,卻非常的單純,因為array自帶GetLength(),本篇主要是針對C/C++做比較。

Introduction

 1/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : ArrayPassToFunction.cs
 5Compiler    : Visual Studio 2005 / C# 2.0
 6Description : Demo how to pass array to function
 7Release     : 02/09/2007 1.0
 8*/

 9using System;
10
11class 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[] {012};
20    func(ia);
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)

posted on 2007-02-09 22:01  真 OO无双  阅读(4198)  评论(0编辑  收藏  举报

导航