Finished Creating an Extension Method

2_linq_and_csharp
jason.zhu 2021-02-24 13:49:26 +11:00
parent 7ac104a924
commit 7756047de1
2 changed files with 22 additions and 0 deletions

20
Features/MyLinq.cs 100644
View File

@ -0,0 +1,20 @@
using System.Collections.Generic;
namespace Features
{
public static class MyLinq
{
// Implement a customized LINQ using extension
public static int MyLinqCount<T>(this IEnumerable<T> sequence)
{
// Any IEnumerable<T> instance can use this method now as extension
int count = 0;
foreach (var item in sequence)
{
count += 1;
}
return count;
}
}
}

View File

@ -20,6 +20,8 @@ namespace Features
new Employee {Id = 3, Name = "Alex"} new Employee {Id = 3, Name = "Alex"}
}; };
Console.WriteLine(sales.MyLinqCount());
var enumerator = developers.GetEnumerator(); var enumerator = developers.GetEnumerator();
while (enumerator.MoveNext()) while (enumerator.MoveNext())