C# 程式,從方法中返回陣列


在 Join 方法下呼叫一個方法,以連線單詞 −

string.Join(" ", display())

現在設定一個字串陣列 −

string[] str = new string[5];

新增單個元素 −

str[0] = "My";
str[1] = "name";
str[2] = "is";
str[3] = "Brad";
str[4] = "Pitt";

並從該方法返回相同的字串陣列 −

return str;

完整的程式碼如下 −

示例

 線上演示

using System;

public class Demo {
   public static void Main() {
      Console.WriteLine(string.Join(" ", display()));
   }
   static string[] display() {
      string[] str = new string[5];

      // string array elements
      str[0] = "My";
      str[1] = "name";
      str[2] = "is";
      str[3] = "Brad";
      str[4] = "Pitt";
      return str;
   }
}

輸出

My name is Brad Pitt

更新於: 22-Jun-2020

10K+ 瀏覽量

開啟你的 職業生涯

完成課程,獲得認證

開始吧
廣告
© . All rights reserved.