C# program to return an array from methods


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements