C# program to return an array from methods

CsharpProgrammingServer Side Programming

Call a method under Join method to join words −

string.Join(" ", display())

Now set a string array −

string[] str = new string[5];

Add individual elements −

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

And return the same string array from method −

return str;

The following is the complete code −

Example

 Live Demo

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;
   }
}

Output

My name is Brad Pitt
raja
Published on 04-Sep-2018 11:41:27
Advertisements