Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Returning Arrays from Methods | Functions Returning Arrays & Refactoring Loops
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Methods & Modular Thinking

bookReturning Arrays from Methods

When working with collections of data in C#, you often need to perform operations that result in more than a single value. Rather than returning one item at a time, you can design methods that return arrays. This approach is especially useful when you want to generate, process, or retrieve multiple values at once, such as creating a list of even numbers, names, or results from calculations. Returning arrays from methods helps you handle groups of related data efficiently and keeps your code organized and modular.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int limit = 10; int[] evenNumbers = GenerateEvenNumbers(limit); Console.WriteLine("Even numbers up to " + limit + ":"); foreach (int num in evenNumbers) { Console.Write(num + " "); } Console.WriteLine(); } public static int[] GenerateEvenNumbers(int max) { int size = max / 2; int[] evens = new int[size]; int index = 0; for (int i = 2; i <= max; i += 2) { evens[index] = i; index++; } return evens; } } }

In this example, you saw how to create an array inside a method, fill it with values, and return it to the caller. The GenerateEvenNumbers method calculates how many even numbers are needed, creates an array of that size, fills it with even numbers up to the limit, and then returns the array. From the calling code (Main), you can access the entire array, loop through its elements, and use the values as needed. This pattern allows you to encapsulate logic for producing a collection of results, making your code easier to reuse and maintain.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string[] days = GetDaysOfWeek(); Console.WriteLine("Days of the week:"); foreach (string day in days) { Console.WriteLine(day); } } public static string[] GetDaysOfWeek() { string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; return days; } } }
Note
Definition

Definition: An array is a data structure that holds a fixed-size sequence of elements of the same type. Unlike methods that return a single value, methods that return arrays can provide multiple related values at once, making it easier to handle collections of data together.

1. What is the correct way to specify that a method returns an array of integers?

2. Why might you want a method to return an array instead of a single value?

3. Fill in the blanks to complete the method so it returns an array of names.

question mark

What is the correct way to specify that a method returns an array of integers?

Select the correct answer

question mark

Why might you want a method to return an array instead of a single value?

Select the correct answer

question-icon

Fill in the blanks to complete the method so it returns an array of names.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookReturning Arrays from Methods

Scorri per mostrare il menu

When working with collections of data in C#, you often need to perform operations that result in more than a single value. Rather than returning one item at a time, you can design methods that return arrays. This approach is especially useful when you want to generate, process, or retrieve multiple values at once, such as creating a list of even numbers, names, or results from calculations. Returning arrays from methods helps you handle groups of related data efficiently and keeps your code organized and modular.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int limit = 10; int[] evenNumbers = GenerateEvenNumbers(limit); Console.WriteLine("Even numbers up to " + limit + ":"); foreach (int num in evenNumbers) { Console.Write(num + " "); } Console.WriteLine(); } public static int[] GenerateEvenNumbers(int max) { int size = max / 2; int[] evens = new int[size]; int index = 0; for (int i = 2; i <= max; i += 2) { evens[index] = i; index++; } return evens; } } }

In this example, you saw how to create an array inside a method, fill it with values, and return it to the caller. The GenerateEvenNumbers method calculates how many even numbers are needed, creates an array of that size, fills it with even numbers up to the limit, and then returns the array. From the calling code (Main), you can access the entire array, loop through its elements, and use the values as needed. This pattern allows you to encapsulate logic for producing a collection of results, making your code easier to reuse and maintain.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string[] days = GetDaysOfWeek(); Console.WriteLine("Days of the week:"); foreach (string day in days) { Console.WriteLine(day); } } public static string[] GetDaysOfWeek() { string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; return days; } } }
Note
Definition

Definition: An array is a data structure that holds a fixed-size sequence of elements of the same type. Unlike methods that return a single value, methods that return arrays can provide multiple related values at once, making it easier to handle collections of data together.

1. What is the correct way to specify that a method returns an array of integers?

2. Why might you want a method to return an array instead of a single value?

3. Fill in the blanks to complete the method so it returns an array of names.

question mark

What is the correct way to specify that a method returns an array of integers?

Select the correct answer

question mark

Why might you want a method to return an array instead of a single value?

Select the correct answer

question-icon

Fill in the blanks to complete the method so it returns an array of names.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1
some-alt