Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 求数组中单词最小和最大长度的最短方法_C#_Lambda - Fatal编程技术网

C# 求数组中单词最小和最大长度的最短方法

C# 求数组中单词最小和最大长度的最短方法,c#,lambda,C#,Lambda,我有以下数组 string[] words = { "cherry", "apple", "blueberry", "banana", "mango", "orange", "pineapple" }; 我想找到字母表的Max和Min编号。e、 g.Max=9(菠萝)和Min=5(苹果) 哪种方法最短。您可以使用和方法: 您可以使用和方法: 最有效的方法是简单地循环字符串: int min = Int32.MaxValue; int max = 0; foreach (s in words)

我有以下数组

string[] words = { "cherry", "apple", "blueberry", "banana", "mango", "orange", "pineapple" };
我想找到字母表的
Max
Min
编号。e、 g.
Max=9(菠萝)和
Min=5(苹果)

哪种方法最短。

您可以使用和方法:

您可以使用和方法:


最有效的方法是简单地循环字符串:

int min = Int32.MaxValue;
int max = 0;
foreach (s in words) {
   min = Math.Min(min, s.Length);
   max = Math.Max(max, s.Length);
}

最有效的方法是简单地循环字符串:

int min = Int32.MaxValue;
int max = 0;
foreach (s in words) {
   min = Math.Min(min, s.Length);
   max = Math.Max(max, s.Length);
}

美丽的。我甚至不知道你可以用C来表达功能性的东西,真漂亮。我甚至不知道你可以用C来表达这样的函数。