我在学校时正在使用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/