Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V3220. The result of the LINQ method...
menu mobile close menu
Additional information
toggle menu Contents

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

Apr 02 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

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
close form
Free PVS‑Studio license for Microsoft MVP specialists
close form
To get the licence for your open-source project, please fill out this form
close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam