[英]C# - string[] does not contain a definition for 'SkipLast' and no accessible extension method
.NET Framework 4.7.2. .NET 框架 4.7.2。 Visual Studio 2019. This issue is not specifically addressed in other posts about this error. Visual Studio 2019。有关此错误的其他帖子中未专门解决此问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stuff
{
class Program
{
static void Main(string[] args)
{
string firstPart = "ABC 123 XYZ";
string firstPartMinusLast = string.Join(" ", firstPart.Split(' ').SkipLast(1));
Console.WriteLine(firstPartMinusLast);
}
}
}
I get an Intellisense error on SkipLast
:我在SkipLast
上收到 Intellisense 错误:
string[] does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'string[]' could be found (are you missing a using directive or an assembly reference?) string[] 不包含“SkipLast”的定义,并且找不到接受“string[]”类型的第一个参数的可访问扩展方法“SkipLast”(您是否缺少 using 指令或程序集引用?)
Per any documentation I can find online, using System.Linq
is supposed to cover this method?根据我可以在网上找到的任何文档, using System.Linq
应该涵盖这种方法吗?
If you check the purple box at the top of the Enumerable.SkipLast
documentation , you'll see that it is not available in .NET Framework 4.7.2如果您选中Enumerable.SkipLast
文档顶部的紫色框,您会看到它在 .NET Framework 4.7.2 中不可用
For what it's worth, the source code is here , so you can just add it yourself.对于它的价值,源代码在这里,所以你可以自己添加它。 The logic is brilliant:逻辑很精彩:
count
items are taken from source
and added to a queue, then one is dequeued and returned.在第一次调用枚举器时, count
项从source
中获取并添加到队列中,然后一个出列并返回。source
, an item is dequeued and returned.在随后的调用中,下一个项目被添加到队列中,如果这不是source
中的最后一个项目,则一个项目被出列并返回。source
(which we wanted to skip), and it was done without knowing ahead of time how many items source
contained.完成后,队列包含source
中最后一项的缓存(我们想跳过),并且在不提前知道source
包含多少项的情况下完成。public static class Extensions
{
public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source, int count)
{
var queue = new Queue<T>();
using (var e = source.GetEnumerator())
{
while (e.MoveNext())
{
if (queue.Count == count)
{
do
{
yield return queue.Dequeue();
queue.Enqueue(e.Current);
} while (e.MoveNext());
}
else
{
queue.Enqueue(e.Current);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.