Collections search
During interviews, we often hear — or say ourselves — that searching in an array is slower than in a hashtable. Some might even recall that array search has linear complexity, or O(n), while a hash table has constant complexity, O(1). But does this hold true in practice? What if there are situations where searching in an array turns out to be faster? Let's not rush to conclusions.
Collection initializer: List<T>
We're all used to write new List<int> { 1, 2, 3, 4 } or new int[] { 1, 2, 3, 4} to initialize collections with
values. Syntactically, they look similar, but the behavior is different, and you should be careful if you are worried
about
performance.
Удаляем пробелы из строки
Еще одна популярная задача при работе со строками — удалить из них пробельные символы. Можно представить, что нам нужно очистить пользовательский ввод (удалить пробелы вначале и конце строк в имени) или минифицировать json-объект. .NET предоставляет нам несколько возможностей для решения этой задачи, давайте рассмотрим самые популярные и попробуем найти наиболее эффективные. Заодно проверим, какие изменения произошли в новой версии .NET 10.
Инициализатор коллекций: List<T>
Мы все привыкли писать new List<int> { 1, 2, 3, 4 } или new int[] { 1, 2, 3, 4}, чтобы инициализировать коллекции какими-то значениями. Синтаксически это выглядит похоже, но поведение отличается, и вам следует быть осторожными, если вы заботитесь о производительности.
String of Digits
A popular task is to check if a string contains only digits. For example, you need to check if the user entered the correct phone number, index or a tax code of an organization. There are several ways to solve this task, which differ in their efficiency. Let's take a look at the most popular ones.
Строка из чисел
Популярная задача — определить, состоит ли строка только из числовых символов. Например, нужно проверить, что пользователь правильно ввел номер телефона, индекс или ИНН организации. Сделать это можно несколькими способами, которые отличаются своей эффективностью. Давайте рассмотрим самые популярные из них.
Any() vs Count: part 2
In Part 1, we compared Any() and Count methods for different collections and proposed optimization approaches.
Any() vs Count: часть 2
В первой части мы сравнивали методы Any() и Count для различных коллекций и предложили вариант оптимизаций.
Прошло достаточно времени, чтобы провести повторное сравнение.
List initializer
We all used to type new List<int> { 1, 2, 3, 4 } or new int[] { 1, 2, 3, 4} to initialize collections with some values. It looks similar in syntax but differs in behavior, and you should be careful if you are concerned about performance.
Harmful collection transformations. Part 3: collections
Starting with string in the first post we continue to study examples with collection transformations and how they affect our applications.