简体   繁体   English

C# TakeLast Extension for .Net Framework 4.6 及更新版本

[英]C# TakeLast Extension for .Net Framework 4.6 and newer versions

I'm looking for help finding an extension method for TakeLast that someone has written that can work for older versions of.Net Framework like 4.6 and higher.我正在寻求帮助,找到有人编写的 TakeLast 的扩展方法,该方法适用于 .Net Framework 的旧版本,如 4.6 及更高版本。 The default TakeLast method only works with my project for.Net 5 and.Net 6 but I'm trying to allow my program to be run on.Net Framework and the only thing I haven't been able to figure out is how to workaround the lack of TakeLast默认的 TakeLast 方法仅适用于我的 .Net 5 和 .Net 6 项目,但我试图让我的程序在.Net Framework 上运行,我唯一不知道的是如何解决缺乏 TakeLast

You can implement your own extension method, something like this:您可以实现自己的扩展方法,如下所示:

public static partial class EnumearbleExtensions {
  public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int count) {
    if (null == source)
      throw new ArgumentNullException(nameof(source));
    if (count < 0)
      throw new ArgumentOutOfRangeException(nameof(count));

    if (0 == count)
      yield break;

    Queue<T> result = new Queue<T>();

    foreach (T item in source) {
      if (result.Count == count)
        result.Dequeue();

      result.Enqueue(item);
    }

    return result;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用较早版本的目标.NET Framework DLL和较新版本的.NET Framework的C#缺点 - C# Disadvantage of using earlier targeted .NET Framework DLL with newer versions of .NET Framework 在构建旧的.NET Framework版本时使用较新的C#功能是否安全? - Is it safe to use newer C# features when building for older .NET Framework versions? 在.NET Framework 4.6中使用C#的SIMD操作速度较慢 - using SIMD operation from C# in .NET framework 4.6 is slower C# - 不同的.NET框架版本 - C# - different .NET framework versions 更改线程文化 .NET Framework 4.6 及更高版本 - Change thread culture .NET Framework 4.6 and later versions 如何使用Framework 4.6在C#上解析IPAddress - How to Parse IPAddress on C# with Framework 4.6 无法在C#中使用SslStream接受.net框架4.6的TLS 1.2协议 - Can't get SslStream in C# to accept TLS 1.2 protocol with .net framework 4.6 如何在C#.Net Framework 4.6 WPF App中编写Python 3词典 - How to write Python 3 dictionary in C# .Net Framework 4.6 WPF App 各种Windows版本或CPU上的C#/。NET框架可用性 - C#/.NET framework availability on various Windows versions or CPUs C# 错误索引超出数组范围(Visual Studio、Winform、.NET Framework 4.6) - C# Error Index was outside the bounds of the array (Visual Studio, Winform, .NET Framework 4.6)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM