return all strings with minimum length from a list in csharp

To return all strings with the minimum length from a list in C#, you can use LINQ and conditional statements as follows:

main.cs
using System.Linq;

public List<string> GetMinLengthStrings(List<string> inputList)
{
    List<string> result = new List<string>();
    int minLength = inputList.Min(str => str.Length); // find minimum length

    foreach (string str in inputList)
    {
        if (str.Length == minLength) // check if string has minimum length
        {
            result.Add(str); // add string to result list
        }
    }

    return result;
}
435 chars
18 lines

This code finds the minimum length of the strings in the input list using LINQ's Min function. It then iterates through the input list to check which strings have the minimum length and adds them to a result list. Finally, it returns the result list.

gistlibby LogSnag