🔷 C#

Métodos de matrices y colecciones en C# (.NET): guía detallada

What is it and why? (a real-life analogy)

Imagine you are a librarian. You have a huge bookshelf with books. Each book is a separate data element (number, string, object). The bookshelf itself is an array or collection.

Before, to find a book by title, you had to manually go through each shelf, pick up each book, read its title, and put it back. It was slow, boring, and easy to make mistakes. What if you needed to sort all the books by publication year? Or find the thickest one? Or delete all books with torn pages? Without special tools, this turns into a real nightmare.

Array and collection methods are like a set of magic buttons on your librarian's remote control. You press one button, and all books are automatically sorted. You press another, and all books by a specific author are found. A third button, and you get a list of just the titles, without extra information.

In programming, these methods allow us to not write loops manually for every simple operation. Instead of writing 10 lines of code to find an element, we write one line with the Find method. Instead of creating a new array and manually copying filtered elements into it, we use Where. This makes the code shorter, clearer, and safer.

Important: Array and collection methods are not just "syntactic sugar." They are optimized by Microsoft professionals, work faster than manual loops in many scenarios, and contain built-in error handling.

Theory: how it works internally

To understand how methods work, you need to understand two key concepts: delegates and lambda expressions. Don't be scared by the scary names — it's actually simple.

Delegates are "containers for functions"

Imagine you want your robot librarian to be able to execute different commands: "find a book", "sort", "delete". Instead of hardcoding each command into the robot, you give it a slot into which you can insert any command. This slot is a delegate.

In C#, a delegate is a type that can store a reference to a method. For example, the Func<int, bool> delegate can store any method that takes a number and returns true or false.

Lambda expressions are "functions on the fly"

Instead of writing a separate method for every simple operation, we can create a function "right on the spot" without giving it a name. This is a lambda expression. It looks like this:

// Regular method
bool IsEven(int number) {
    return number % 2 == 0;
}

// Same thing, but with a lambda expression
(int number) => number % 2 == 0;

The arrow => is read as "goes to" or "returns". Everything to the left is input parameters, to the right is the expression that will be evaluated and returned.

How methods process collections

Most array and collection methods work on the same principle:

  1. They take each element of the collection one by one.
  2. They apply your function (lambda expression) to it.
  3. Depending on the method, they either return a result (e.g., true/false for Any) or form a new collection (e.g., for Where).

It is important to understand that many methods (especially from LINQ) use deferred execution. This means they are not executed immediately, but only when you start iterating over the result. This saves memory and time, but can lead to surprises if you are not aware of this feature.

Remember: Arrays in C# have a fixed size. Collections (List, Dictionary, HashSet) can grow and shrink. The methods for them are largely similar, but there are important differences.

Syntax in C# (.NET): all forms and variants

Let's look at the main methods you will need. I have divided them into groups by purpose.

1. Search and check methods

Method What it does Example Result
Contains Checks if an element exists in the collection list.Contains(5) true or false
Any Checks if at least one element satisfies a condition list.Any(x => x > 10) true or false
All Checks if ALL elements satisfy a condition list.All(x => x > 0) true or false
Find Finds the FIRST element satisfying a condition list.Find(x => x.Name == "Vasya") Element or null
FindAll Finds ALL elements satisfying a condition list.FindAll(x => x.Age > 18) New list
IndexOf Finds the index of the first occurrence of an element list.IndexOf("Hello") Number (index) or -1

2. Transformation methods

<
Method What it does Example Result
Select Transforms each element according to a given rule