简体   繁体   English

C# - string[] 不包含“SkipLast”的定义并且没有可访问的扩展方法

[英]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:逻辑很精彩:

  • On the first call to the enumerator, count items are taken from source and added to a queue, then one is dequeued and returned.在第一次调用枚举器时, count项从source中获取并添加到队列中,然后一个出列并返回。
  • On subsequent calls, the next item is added to the queue, and if that wasn't the last item in source , an item is dequeued and returned.在随后的调用中,下一个项目被添加到队列中,如果这不是source中的最后一个项目,则一个项目被出列并返回。
  • When it's done, the queue contains a cache of the last items in the 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.

相关问题 c# 不包含定义和扩展方法 - c# does not contain a definition for and no extension method C# 错误:1061“PlayerLook”不包含“ProcessLook”的定义并且没有可访问的扩展方法“ProcessLook” - C# error: 1061 'PlayerLook' does not contain a definition for 'ProcessLook' and no accessible extension method 'ProcessLook' C# 控制台应用程序集合不包含 object class 的定义并且没有可访问的扩展方法 - C# console app Collection does not contain definition for object class and no accessible extension method 不包含 &#39;AddComponent&#39; 的定义,并且在 Unity 中没有可访问的扩展方法 &#39;AddComponent&#39; 错误 - Does not contain a definition for 'AddComponent' and no accessible extension method 'AddComponent' error in Unity 'Avro解串器<genericrecord> ' 不包含 'AsSyncOverAsync' 的定义并且没有可访问的扩展方法</genericrecord> - 'AvroDeserializer<GenericRecord>' does not contain a definition for 'AsSyncOverAsync' and no accessible extension method “BitmapImage”不包含“SetSource”的定义,也没有可访问的扩展方法“SetSource” - 'BitmapImage' does not contain a definition for 'SetSource' and no accessible extension method 'SetSource' DbSet <xxx> &#39;不包含&#39;Update&#39;的定义,也没有可访问的扩展方法&#39;Update&#39; - DbSet<xxx>' does not contain a definition for 'Update' and no accessible extension method 'Update' “DateTime”不包含“HasValue”的定义,也没有可访问的扩展方法 - 'DateTime' does not contain a definition for 'HasValue' and no accessible extension method HttpResponseMessage' 不包含 'GetAwaiter' 的定义,并且没有可访问的扩展方法 'GetAwaiter' - HttpResponseMessage' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' 用户经理<XYZ> &#39; 不包含 &#39;CreateIdentityAsync&#39; 的定义并且没有可访问的扩展方法 - UserManager<XYZ>' does not contain a definition for 'CreateIdentityAsync' and no accessible extension method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM