You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.6 KiB
C#

using System.Linq;
namespace asyncWhenAll
{
public static class ExtMethod
{ //扩展语法
public static int SumMe(this IEnumerable<int> source)
{
int sum = 0;
foreach (var item in source)
{
sum += item;
}
return sum;
}
//扩展语法
public static IEnumerable<int> WhereMe(this IEnumerable<int> source,Func<int,bool> func)
{
foreach (var item in source)
{
if (func(item)) yield return item;
}
}
}
internal class Program
{
static async Task Main(string[] args)
{
string[] files = Directory.GetFiles(@"C:\Users\61778\Desktop\enfi-boot-order\enfi-boot-module-system\src\main\java\org\enfi\config\init");
Task<int>[] tasks = new Task<int>[files.Length];
for (int i = 0; i < files.Length; i++)
{
tasks[i] = ReadCharsCount(files[i]);
}
int[] rs = await Task.WhenAll(tasks);
Console.WriteLine(rs.SumMe());
foreach (var item in rs.WhereMe(n=>n>1200))
{
Console.WriteLine(item);
}
string cj = "61,90,100,99,18,22,38,66,80,93,55,50,89";
Console.WriteLine(cj.Split(",").Average(s=>int.Parse(s)));
}
static async Task<int> ReadCharsCount(string filename)
{
string s = await File.ReadAllTextAsync(filename);
return s.Length;
}
}
}