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

bookReturning Arrays from Methods

Note
Definition
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; } } }
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; } } }
question mark

Select the correct answer

question mark

Select the correct answer

question-icon
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you show me an example of the `GenerateEvenNumbers` method?

How do I use the returned array in my main program?

What are some other scenarios where returning arrays from methods is useful?

bookReturning Arrays from Methods

Veeg om het menu te tonen

Note
Definition
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; } } }
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; } } }
question mark

Select the correct answer

question mark

Select the correct answer

question-icon
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1
some-alt