Array Methods: TypeScript vs C# 🧮
Tags: typescript, csharp, arrays, programming
Published: 2024-04-12 22:41:58

Arrays are fundamental in programming, enabling developers to efficiently store and manipulate collections of data. Most programming languages provide a variety of array functions or methods to simplify common tasks such as iteration, transformation, filtering, and aggregation. In this blog post, we’ll explore and compare some of the most commonly used array functions, examining their implementations in both TypeScript and C#.

## forEach
Executes a provided function once for each array element, allowing you to perform a specific action on each element without creating a new array.

```TypeScript
// TypeScript
const array = [1, 2, 3, 4, 5];
array.forEach((element) => {
    console.log(element);
});
```

```C#
// C#
var array = new List<int> { 1, 2, 3, 4, 5 };
array.ForEach(element => {
    Console.WriteLine(element);
});
```

## map
Creates a new array populated with the results of calling a provided function on every element in the calling array. It’s useful for transforming each element in an array based on a specific logic.

```TypeScript
// TypeScript
const array = [1, 2, 3, 4, 5];
const newArray = array.map((element) => {
    return element * 2;
});
console.log(newArray); // Output: [2, 4, 6, 8, 10]
```

```C#
// C#
var array = new List<int> { 1, 2, 3, 4, 5 };
var newArray = array.Select(element => {
    return element * 2;
}).ToList();
Console.WriteLine(string.Join(", ", newArray)); // Output: 2, 4, 6, 8, 10
```

## filter
Creates a new array with all elements that pass the test implemented by the provided function. It’s useful for removing elements that don’t meet a certain condition.

```TypeScript
// TypeScript
const array = [1, 2, 3, 4, 5];
const filteredArray = array.filter((element) => {
    return element > 2;
});
console.log(filteredArray); // Output: [3, 4, 5]
```

```C#
// C#
var array = new List<int> { 1, 2, 3, 4, 5 };
var filteredArray = array.Where(element => {
    return element > 2;
}).ToList();
Console.WriteLine(string.Join(", ", filteredArray)); // Output: 3, 4, 5
```

## reduce
Applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. It’s useful for aggregating the elements of an array into a single value.

```TypeScript
// TypeScript
const array = [1, 2, 3, 4, 5];
const result = array.reduce((accumulator, currentValue) => {
    return accumulator + currentValue;
}, 0);
console.log(result); // Output: 15
```

```C#
// C#
var array = new List<int> { 1, 2, 3, 4, 5 };
var result = array.Aggregate(0, (accumulator, currentValue) => {
    return accumulator + currentValue;
});
Console.WriteLine(result); // Output: 15
```

## find
Returns the first element in the array that satisfies the provided testing function. It’s useful for finding a single element based on a condition.

```TypeScript
// TypeScript
const array = [1, 2, 3, 4, 5];
const foundElement = array.find((element) => {
    return element === 3;
});
console.log(foundElement); // Output: 3
```

```C#
// C#
var array = new List<int> { 1, 2, 3, 4, 5 };
var foundElement = array.Find(element => {
    return element == 3;
});
Console.WriteLine(foundElement); // Output: 3
```

---

There you have it, while the syntax and specific method names may differ between the two languages, the core concepts remain the same. By understanding the similarities and differences in array functions between TypeScript and C#, developers can effectively work with arrays in their projects and leverage the full power of arrays in any programming language.