【问题标题】:C# Ambiguous reference between self built Node<T> \ List<T> Class to system's Node and ListC# 自建 Node<T> \ List<T> 类对系统的 Node 和 List 的不明确引用
【发布时间】:2025-11-21 22:30:01
【问题描述】:

我在 c# 中构建了自己的 Node\List 类,并尝试在新项目中使用它们,但是 vs 似乎给了我无数错误,我无法弄清楚如何解决。

代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Students;
using Nodes;






namespace schoolNodes
{
    class Program
    {

        public static Student Best(List<Student> x)
        {
            Node<Student> p = x.GetFirst();
            Student tal, talmax = null;
            float mem, max = 0;

            while (p != null)
            {
                tal = p.GetValue();
                mem = tal.Avg();
                if (mem > max)
                {
                    max = mem; 
                    talmax = tal; 
                }
                p = p.GetNext();
            }
            return talmax;
        }

        public static int NumTeacherOfClass(string s, List<Teacher> l)
        {
            Node<Teacher> p = l.GetFirst();
            int n = 0;

            while (p != null)
            {
                if (p.GetValue().CheckClass(s))
                    n++;
                p = p.GetNext();
            }

            return n;
        }

无论哪里有 List<...> 它都会给我一个错误。
我将 Node\List 类放入的命名空间称为“Nodes”。

有什么建议吗?谢谢!

【问题讨论】:

    标签: c# list oop compiler-errors nodes


    【解决方案1】:

    您将System.Collections.Generic 与您的Nodes 一起使用,其中包含List 定义。这会导致编译器错误。尝试使用带有命名空间的全名,例如 Nodes.List&lt;Student&gt; 而不是 List&lt;Student&gt;

    【讨论】:

    • 我试过 using Nodes.List; using Nodes.Node; 但它给了我和错误,它要求 List 和 Node 的值所以我尝试了 using Nodes.List&lt;T&gt;; using Nodes.Node&lt;T&gt;; 但它错误地告诉我有一个;&lt;T&gt; 之前缺失。
    • 也许你没有正确理解我,我的意思是“List x”而不是“Nodes.List x”。另请使用错误消息和有关节点类的信息更新您的答案。您可以删除实现细节,只需定义。
    • 非常感谢!它运行良好!有什么建议我下次如何避免这种情况?
    • 在这种情况下,您可以以不同的方式命名您的类或始终使用全名的类。但是这个问题只导致泛型类型,因为在其他情况下,您可以“使用”所需的类,如 using Node = MyNodes.Node
    最近更新 更多