Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to return an array from methods
In C#, methods can return arrays by specifying the array type in the method signature and using the return statement. This allows you to create, populate, and return arrays from methods to be used by the calling code.
Syntax
Following is the syntax for declaring a method that returns an array −
dataType[] MethodName() {
// create and populate array
return arrayVariable;
}
To call the method and use the returned array −
dataType[] result = MethodName();
Returning String Arrays from Methods
You can create a method that returns a string array by declaring the return type as string[]. The method creates an array, populates it with values, and returns it −
Example
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;
}
}
The output of the above code is −
My name is Brad Pitt
Returning Integer Arrays from Methods
Similarly, you can return arrays of other data types like integers. The method signature uses int[] as the return type −
Example
using System;
public class NumberDemo {
public static void Main() {
int[] numbers = GetNumbers();
Console.WriteLine("Array elements: " + string.Join(", ", numbers));
Console.WriteLine("Sum: " + CalculateSum(numbers));
}
static int[] GetNumbers() {
int[] arr = new int[5] { 10, 20, 30, 40, 50 };
return arr;
}
static int CalculateSum(int[] numbers) {
int sum = 0;
foreach (int num in numbers) {
sum += num;
}
return sum;
}
}
The output of the above code is −
Array elements: 10, 20, 30, 40, 50 Sum: 150
Using Array Initialization in Return Methods
You can also use array initialization syntax to create and return arrays more concisely −
Example
using System;
public class FruitDemo {
public static void Main() {
string[] fruits = GetFruits();
Console.WriteLine("Fruits available:");
foreach (string fruit in fruits) {
Console.WriteLine("- " + fruit);
}
}
static string[] GetFruits() {
return new string[] { "Apple", "Banana", "Orange", "Mango", "Grapes" };
}
}
The output of the above code is −
Fruits available: - Apple - Banana - Orange - Mango - Grapes
Key Points
-
The method return type must match the array type being returned (e.g.,
string[],int[]). -
Arrays are reference types, so returning an array returns a reference to the array object.
-
You can use
string.Join()to convert string arrays into formatted strings for display. -
Array initialization syntax
new dataType[] { ... }provides a concise way to create and return arrays.
Conclusion
Returning arrays from methods in C# is achieved by specifying the array type in the method signature and using the return statement. This technique is useful for encapsulating array creation logic and making your code more modular and reusable.
