Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 泛型嵌套类型:无法从X转换为X_C#_Generics - Fatal编程技术网

C# 泛型嵌套类型:无法从X转换为X

C# 泛型嵌套类型:无法从X转换为X,c#,generics,C#,Generics,我正在编写一个处理无向图的类,遇到以下编译时错误: 匹配的最佳重载方法 'Dictionary.EdgeCollection>.AddTVertex,UndirectedGraph.EdgeCollection' 有一些无效的参数 参数2:无法转换 来自UndirectedGraph.AdjaceEntedgeCollection 到UndirectedGraph.AdjaceEntedgeCollection 我可以将问题简化为以下示例: public class UndirectedGrap

我正在编写一个处理无向图的类,遇到以下编译时错误:

匹配的最佳重载方法 'Dictionary.EdgeCollection>.AddTVertex,UndirectedGraph.EdgeCollection' 有一些无效的参数

参数2:无法转换 来自UndirectedGraph.AdjaceEntedgeCollection 到UndirectedGraph.AdjaceEntedgeCollection

我可以将问题简化为以下示例:

public class UndirectedGraph<TVertex, TEdge>
{
    Dictionary<TVertex, EdgeCollection<TVertex, TEdge>> edges;

    class VertexCollection<TVertex, TEdge>
    {
        UndirectedGraph<TVertex, TEdge> graph;

        public VertexCollection(UndirectedGraph<TVertex, TEdge> graph)
        { this.graph = graph; }

        public void Add(TVertex value)
        {
            // Argument 2: cannot convert
            // from 'UndirectedGraph<TVertex,TEdge>.AdjacentEdgeCollection<TVertex,TEdge>'
            //   to 'UndirectedGraph<TVertex,TEdge>.AdjacentEdgeCollection<TVertex,TEdge>'
            this.graph.edges.Add(value, new EdgeCollection<TVertex, TEdge>(this.graph));
        }
    }

    class EdgeCollection<TVertex, TEdge>
    {
        public EdgeCollection(UndirectedGraph<TVertex, TEdge> graph) { }
    }
}

请注意,嵌套类中的TVertex和TEdge与外部类中的TVertex和TEdge不同,我收到警告,指出应该重命名它们。我可以这样做,但这不会影响错误。我认为片段的目的很清楚,那么我如何让它做我想做的事情?我的想法哪里出了问题?

你确定有三个TVertex类型参数和三个TEdge类型参数吗?在我看来,这三者都是相同的,您需要的是:

public class UndirectedGraph<TVertex, TEdge>
{
    Dictionary<TVertex, EdgeCollection> edges;

    class VertexCollection
    {
        UndirectedGraph<TVertex, TEdge> graph;

        public VertexCollection(UndirectedGraph<TVertex, TEdge> graph)
        { this.graph = graph; }

        public void Add(TVertex value)
        {
            this.graph.edges.Add(value, new EdgeCollection(this.graph));
        }
    }

    class EdgeCollection
    {
        public EdgeCollection(UndirectedGraph<TVertex, TEdge> graph) { }
    }
}

您确定有三个TVertex类型参数和三个TEdge类型参数吗?在我看来,这三者都是相同的,您需要的是:

public class UndirectedGraph<TVertex, TEdge>
{
    Dictionary<TVertex, EdgeCollection> edges;

    class VertexCollection
    {
        UndirectedGraph<TVertex, TEdge> graph;

        public VertexCollection(UndirectedGraph<TVertex, TEdge> graph)
        { this.graph = graph; }

        public void Add(TVertex value)
        {
            this.graph.edges.Add(value, new EdgeCollection(this.graph));
        }
    }

    class EdgeCollection
    {
        public EdgeCollection(UndirectedGraph<TVertex, TEdge> graph) { }
    }
}

你的意图到底是什么?看起来不应该让VertexCollection和EdgeCollection双重泛型,而应该在嵌套类中使用外部TVertex和TEdge。@harold这也是我回答的目的;当我发布我的答案时,我没有看到你的评论。你的意图到底是什么?看起来不应该让VertexCollection和EdgeCollection双重泛型,而应该在嵌套类中使用外部TVertex和TEdge。@harold这也是我回答的目的;当我发布我的答案时,我没有看到你的评论。