0Pricing
C# Academy · 课时

可组合性技巧

保持 LINQ 的可组合性:使用小型纯谓词和转换函数,组合委托,添加微型扩展辅助方法,并返回 IEnumerable 管道。

可组合性技巧 是 CoddyKit 上的免费 C# Academy 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。

可组合 LINQ 概览

目标:让 LINQ 易于扩展。

  • 编写小型纯谓词/转换函数
  • 组合函数(不产生副作用)
  • 添加微小的扩展辅助方法
  • 返回 IEnumerable<T> 流水线

复用纯辅助方法

提取小型纯方法,并将其作为方法组传入。通过复用,可使流水线保持简短易读。

using System;
using System.Linq;

public class Program
{
  static bool IsEven(int n) { return n % 2 == 0; }   // pure predicate
  static int Square(int n) { return n * n; }         // pure transform

  public static void Main(string[] args)
  {
    int[] xs = new int[] { 1, 2, 3, 4, 5, 6 };

    var q = xs
      .Where(IsEven)     // reuse predicate
      .Select(Square);   // reuse transform

    foreach (int v in q) Console.WriteLine(v); // 4 16 36
  }
}

组合谓词

通过组合更小的谓词来构建新谓词。这样可以使逻辑易于测试和复用。

using System;
using System.Linq;

public class Program
{
  // Return a predicate n > threshold
  static Func<int, bool> GreaterThan(int threshold)
  {
    return delegate(int n) { return n > threshold; };
  }

  // Combine two predicates with AND
  static Func<int, bool> And(Func<int, bool> a, Func<int, bool> b)
  {
    return delegate(int n) { return a(n) && b(n); };
  }

  static bool IsEven(int n) { return n % 2 == 0; }

  public static void Main(string[] args)
  {
    int[] xs = new int[] { 1, 2, 3, 4, 5, 6 };

    Func<int, bool> gt2 = GreaterThan(2);
    Func<int, bool> evenAndGt2 = And(IsEven, gt2);

    var q = xs.Where(evenAndGt2); // even and > 2 => 4,6

    foreach (int v in q) Console.WriteLine(v);
  }
}

易读的扩展

添加一个小型扩展来表达意图(例如 WhereNot)。请保持简单且无副作用。

using System;
using System.Collections.Generic;
using System.Linq;

public static class LinqEx
{
  public static IEnumerable<T> WhereNot<T>(this IEnumerable<T> src, Func<T, bool> predicate)
  {
    foreach (T item in src)
    {
      if (!predicate(item)) yield return item;
    }
  }
}

public class Program
{
  static bool IsVowel(char c)
  {
    char x = Char.ToLower(c);
    return x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u';
  }

  public static void Main(string[] args)
  {
    char[] letters = new char[] { 'a', 'b', 'c', 'e', 'i' };

    var consonants = letters.WhereNot(IsVowel);

    foreach (char c in consonants) Console.WriteLine(c); // b, c
  }
}

可复用的流水线辅助方法

提供可复用的 IEnumerable<T> 辅助方法。调用方可以继续组合(OrderBy、Take 等)。

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
  static bool IsEven(int n) { return n % 2 == 0; }
  static int Square(int n) { return n * n; }

  // Reusable pipeline helper: filter evens and square
  public static IEnumerable<int> SquaresOfEvens(IEnumerable<int> xs)
  {
    foreach (int x in xs)
    {
      if (IsEven(x)) yield return Square(x);
    }
  }

  public static void Main(string[] args)
  {
    int[] data = new int[] { 1, 2, 3, 4, 5, 6 };

    var pipeline = SquaresOfEvens(data); // composable
    var topTwo = pipeline.OrderByDescending(x => x).Take(2);

    foreach (int v in topTwo) Console.WriteLine(v); // 36, 16
  }
}

可组合性检查清单

提示:

  • 优先使用纯辅助方法;避免在 Select/Where 中产生副作用。
  • 组合谓词和转换,并为它们起清晰的名称。
  • 让辅助方法返回 IEnumerable<T>,以便调用方扩展该链。
  • 尽量延后物化;除非确有需要,否则不要在中间调用 ToList。

保持可组合性的最佳实践

快速检查:哪种做法最能保持 LINQ 流水线的可组合性和可测试性?

总结

总结:提取小型纯辅助方法,组合谓词,添加微小扩展,并返回流水线,以便调用方继续链接。

常见问题解答

「可组合性技巧」课时是免费的吗?

是的 — 「可组合性技巧」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。

「可组合性技巧」这节课中我会学到什么?

保持 LINQ 的可组合性:使用小型纯谓词和转换函数,组合委托,添加微型扩展辅助方法,并返回 IEnumerable 管道。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 C# Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。

「可组合性技巧」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 C# Academy 课中编写并运行代码吗?

能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. GroupBy、Join、Aggregate 与投影(匿名类型)
  2. 具体化(ToList、ToDictionary)与性能提示
  3. 可组合性技巧
← 返回 C# Academy