c# - 错误CS1955 : The member 'System.Collections.Generic.List<System.Action>.Count' cannot be used as a method or delegate

标签 c# syntax-error system

我在学校时正在使用repl.it进行编程。我使用的代码与我在家中使用的代码相同,但出现错误。我不确定我是否犯了一个我没有注意到的小错误,或者是什么,但是我需要帮助。这是我的代码:

  public static void Main (string[] args) 
  {
    Console.ForegroundColor = ConsoleColor.White;

    if (debugMode == true)
    {
      Console.WriteLine("DEBUG ON");
    }
    else if (debugMode == false)
    {
      Console.WriteLine("DEBUG OFF");
    }

    List<Action> actions = new List<Action>();

    actions.Add(() => NormalDay());

    Random random = new Random();

    int selectedAction = random.Next(0, actions.Count());

    actions[selectedAction].Invoke();
  }

最佳答案

让我们详细一点。
List<T>具有一个名为 Count 的属性。由于它是一个属性,因此无需使用方括号即可对其进行访问:

int count = mylist.Count;

extension method类上还有一个 System.Linq.Enumerable ,它在可以枚举的所有对象(即实现 Count() 的任何对象)上定义了 IEnumerable<T> 方法:这包括List<T>。这就像是List<T>上的方法一样被调用。

但是,只有在文件顶部有using System.Linq;时,编译器才会找到此扩展方法。

例如:
using System.Linq;

...
int count = mylist.Count();

(在List<T>的特定情况下,Count()扩展方法将只访问Count属性,因此使用其中任一种都可以。但是使用Count属性更为正常)。

所以区别是,在学校,文件顶部有using System.Linq;,但您并不在家。

关于c# - 错误CS1955 : The member 'System.Collections.Generic.List<System.Action>.Count' cannot be used as a method or delegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59789777/

相关文章:

c# - 确定关闭正在关闭我的应用程序

C# IteropServices 与 C++ dll - 在 excel 中工作?

c# - 关于 C# 中的 string.Intern

android - 在android中关闭屏幕

linux - 如何设置非root用户的cronjob?

c# - 使用 HtmlAgilityPack 设置文本区域值

c# - 在C#中写行错误

连接 CTE 时 SQL Server 语法错误

vba - 由于语法错误(录制时)或对象定义错误(运行时)而无法运行 VBA

c# - 以编程方式更改系统日期