Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
V3220. The result of the LINQ method wi…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V3220. The result of the LINQ method with deferred execution is never used. The method will not be executed.

02 Avr 2025

The analyzer has detected that the result of the LINQ method with deferred execution is not executed.

The example:

public static void PrintNames(List<string> names, char startLetter)
{
  int it = 0;
  var filteredNames = names.Where(name => 
                      {
                        ++it;
                        Console.WriteLine($"{it}) {name}");
                        return name.StartsWith(startLetter); 
                      });

  Console.WriteLine($"Type: '{filteredNames.GetType()}'");
  Console.WriteLine(String.Join(',', names));
}

A delegate that both applies a filter condition and prints elements along with their positions from the original collection to the console is passed to the Where method.

However, neither the output of names with positions nor filtering will occur. Instead of filteredNames, names is passed to theString.Join method. As a result, the filtered collection is not printed to the console, and filtering does not occur at all. This behavior occurs because when Where is called, filtering is not executed immediately but is deferred. So, the delegate code will not be executed until the collection is iterated over.

You can learn more about deferred execution here.

Note that after calling Where via the GetType method, the collection type is printed. When this method is called, iteration over the collection is not executed, which means that the delegate passed to Where will not operate either.

To fix this error, pass filteredNames instead of names to String.Join:

public static void PrintNames(List<string> names, char startLetter)
{
  int it = 0;
  var filteredNames = names.Where(name => 
                      {
                        ++it;
                        Console.WriteLine($"{it}) {name}");
                        return name.StartsWith(startLetter); 
                      });

  Console.WriteLine($"Type: '{filteredNames.GetType()}'");
  Console.WriteLine(String.Join(',', filteredNames));
}

When String.Join is called, filteredNames is iterated over, triggering the collection filtering. So, the delegate code will be executed, and the elements with positions as well as the filtered collection are printed to the console.

This diagnostic is classified as:

close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam