简体   繁体   English

List<> 不能使用 C#

[英]List<> cannot be used C#

I have this error and I don't have any idea what's happening, I'm new learning C# and where I'm learning they use this:我有这个错误,我不知道发生了什么,我是新学习 C# 并且我正在学习他们使用这个:

using System;

namespace AgentSmith
{
    class AgentSmith_missions
    {
        public static void Main(string[] args)
        {
            double[] array = {1.1, 2.2, 3.3};
            List<double> times  = new List<double>();
            times.Add(4.20);
            times.Add(7.30);
            times.Insert(1, 9.35);
            var four = times.IndexOf(4.20);
            times.Remove(times[four]);
            double seven = times.Contains(7.30);
            string s = String.Join(", ", times);
            Console.WriteLine(s + seven);
       }
    }
}

The console error is this:控制台错误是这样的:

/Users.../AgentSmith.cs(10,13): error CS0246: The type or namespace name 'List<>' could not be found (are you missing a using directive or an assembly reference?)    
/Users.../AgentSmith.cs(10,39): error CS0246: The type or namespace name 'List<>' could not be found (are you missing a using directive or an assembly reference?)

Here is a keyboard shortcut that'll make this easier to fix:这是一个键盘快捷键,可以更轻松地解决此问题:

Control .

That is, the Control key plus the period .也就是说, Control键加上句号. key.钥匙。

That keyboard shortcut tries to resolve the item under the cursor, which often is a matter of adding a using to bring a namespace into scope.该键盘快捷键尝试解析光标下的项目,这通常是添加 using 将命名空间引入作用域的问题。 If you get an error squiggly on something that you know is defined, click on it and hit Control .如果您在知道已定义的内容上出现波浪形错误,请单击它并点击Control . and look for the missing namespace.并查找丢失的命名空间。

要添加这个using在你的类的顶部:

using System.Collections.Generic;

You have coded for a list in your Main function.您已在Main函数中为列表编码。 But you also need to add a reference to the List class so that your compiler knows what to do with List .但是您还需要添加对List类的引用,以便您的编译器知道如何处理List Just add the following to the top of your file:只需将以下内容添加到文件顶部:

using System.Collections.Generic;

In addition, it looks like this line could cause a problem as well:此外,看起来这一行也可能导致问题:

double seven = times.Contains(7.30);

Contains returns a bool . Contains返回一个bool It's true or false based on whether or not the list contains that value.根据列表是否包含该值是true还是false What you are probably looking for here is First , which returns the first value that matches:您可能在这里寻找的是First ,它返回匹配的第一个值:

double seven = times.First(x => x == 7.30);

You have to add System.Collections.Generic;你必须添加 System.Collections.Generic; add also times.Contains(7.30);还添加 times.Contains(7.30); return a booleen not a double.返回一个布尔值而不是双倍。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM