Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Filtering Arrays | Searching and Filtering Arrays
Quizzes & Challenges
Quizzes
Challenges
/
C# Arrays

bookFiltering Arrays

Filtering an array means selecting only those elements that meet specific conditions, such as being above a certain value or matching a pattern. This process is called filtering because you "filter out" the elements that do not meet your criteria, leaving you with only the elements you want to keep. Filtering is a common task when you want to work with a subset of data from a larger collection.

Program.cs

Program.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 10, 55, 23, 67, 89, 34, 51, 42 }; Console.WriteLine("Numbers greater than 50:"); for (int i = 0; i < numbers.Length; i++) { if (numbers[i] > 50) { Console.WriteLine(numbers[i]); } } } } }

To understand how filtering works in practice, look at each step in the code above. First, you have an array of integers called numbers. You want to display only the numbers that are greater than 50. To do this, you loop through the array using a for loop. Inside the loop, you use an if statement to check if the current number is greater than 50. If the condition is true, you print that number. This way, only the numbers that meet your condition—being greater than 50—are printed to the console.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 10, 55, 23, 67, 89, 34, 51, 42 }; List<int> filteredList = new List<int>(); for (int i = 0; i < numbers.Length; i++) { if (numbers[i] > 50) { filteredList.Add(numbers[i]); } } int[] filteredArray = filteredList.ToArray(); Console.WriteLine("Filtered array:"); for (int i = 0; i < filteredArray.Length; i++) { Console.WriteLine(filteredArray[i]); } } } }
Note
Note

Arrays in C# have a fixed size. When you filter elements, you often do not know in advance how many will meet your condition. A common approach is to use a temporary List<T> to collect the filtered elements, then convert the list to an array if needed.

1. Why can't you simply remove elements from a C# array?

2. What is a common approach to store filtered results from an array?

3. How do you check if an element meets a filtering condition?

question mark

Why can't you simply remove elements from a C# array?

Select the correct answer

question mark

What is a common approach to store filtered results from an array?

Select the correct answer

question mark

How do you check if an element meets a filtering condition?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookFiltering Arrays

Swipe um das Menü anzuzeigen

Filtering an array means selecting only those elements that meet specific conditions, such as being above a certain value or matching a pattern. This process is called filtering because you "filter out" the elements that do not meet your criteria, leaving you with only the elements you want to keep. Filtering is a common task when you want to work with a subset of data from a larger collection.

Program.cs

Program.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 10, 55, 23, 67, 89, 34, 51, 42 }; Console.WriteLine("Numbers greater than 50:"); for (int i = 0; i < numbers.Length; i++) { if (numbers[i] > 50) { Console.WriteLine(numbers[i]); } } } } }

To understand how filtering works in practice, look at each step in the code above. First, you have an array of integers called numbers. You want to display only the numbers that are greater than 50. To do this, you loop through the array using a for loop. Inside the loop, you use an if statement to check if the current number is greater than 50. If the condition is true, you print that number. This way, only the numbers that meet your condition—being greater than 50—are printed to the console.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 10, 55, 23, 67, 89, 34, 51, 42 }; List<int> filteredList = new List<int>(); for (int i = 0; i < numbers.Length; i++) { if (numbers[i] > 50) { filteredList.Add(numbers[i]); } } int[] filteredArray = filteredList.ToArray(); Console.WriteLine("Filtered array:"); for (int i = 0; i < filteredArray.Length; i++) { Console.WriteLine(filteredArray[i]); } } } }
Note
Note

Arrays in C# have a fixed size. When you filter elements, you often do not know in advance how many will meet your condition. A common approach is to use a temporary List<T> to collect the filtered elements, then convert the list to an array if needed.

1. Why can't you simply remove elements from a C# array?

2. What is a common approach to store filtered results from an array?

3. How do you check if an element meets a filtering condition?

question mark

Why can't you simply remove elements from a C# array?

Select the correct answer

question mark

What is a common approach to store filtered results from an array?

Select the correct answer

question mark

How do you check if an element meets a filtering condition?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
some-alt