C# 在泛型函数中使用集合

C# 在泛型函数中使用集合,c#,generics,collections,C#,Generics,Collections,我试图对两种集合使用一个泛型函数,其中我调用了Add方法 下面是我的代码: using System; using System.Collections; namespace CollectionsApplication { class Program { static void AddElement<T>(ref T container, string key, string value) { contain

我试图对两种集合使用一个泛型函数,其中我调用了Add方法

下面是我的代码:

using System;
using System.Collections;

namespace CollectionsApplication
{
    class Program
    {
        static void AddElement<T>(ref T container, string key, string value)
        {
            container.Add(key, value);
        }

        static void Main(string[] args)
        {
            SortedList s1 = new SortedList();
            Hashtable  h1 = new Hashtable();

            AddElement<SortedList>(ref s1, "001", "Zara Ali");
            AddElement<Hashtable>(ref h1, "001", "Zara Ali");
        }
    }
}
使用系统;
使用系统集合;
命名空间集合应用程序
{
班级计划
{
静态void AddElement(参考T容器、字符串键、字符串值)
{
容器。添加(键、值);
}
静态void Main(字符串[]参数)
{
SortedList s1=新的SortedList();
Hashtable h1=新的Hashtable();
附录(参考s1,“001”,“Zara Ali”);
附录(参考h1,“001”,“Zara Ali”);
}
}
}
下面是错误:

错误CS1061:“T”不包含“添加”和“否”的定义 扩展方法“Add”接受类型为“T”的第一个参数

那么,是否可以执行此操作?如果可能,如何修复它


提前谢谢。

这里的问题是T可以是任何东西(例如int),并且不能保证有Add方法

static void AddElement<T>(ref T container, string key, string value)
    where T : IDictionary 
{
    container.Add(key, value);
}
您需要将T约束到具有Add方法的对象

static void AddElement<T>(ref T container, string key, string value)
    where T : IDictionary 
{
    container.Add(key, value);
}
静态void AddElement(参考T容器、字符串键、字符串值)
其中T:IDictionary
{
容器。添加(键、值);
}

这里的问题是T可以是任何东西(例如int),并且不能保证有Add方法

static void AddElement<T>(ref T container, string key, string value)
    where T : IDictionary 
{
    container.Add(key, value);
}
您需要将T约束到具有Add方法的对象

static void AddElement<T>(ref T container, string key, string value)
    where T : IDictionary 
{
    container.Add(key, value);
}
静态void AddElement(参考T容器、字符串键、字符串值)
其中T:IDictionary
{
容器。添加(键、值);
}

为什么不像这样简化它呢

using System;
using System.Collections;

namespace CollectionsApplication
{
    class Program
    {
        static void AddElement(IDictionary container, string key, string value)
        {
            container.Add(key, value);
        }

        static void Main(string[] args)
        {
            SortedList s1 = new SortedList();
            Hashtable  h1 = new Hashtable();

            AddElement(s1, "001", "Zara Ali");
            AddElement(h1, "001", "Zara Ali");
        }
    }
}

为什么不让它变得更容易,就像这样

using System;
using System.Collections;

namespace CollectionsApplication
{
    class Program
    {
        static void AddElement(IDictionary container, string key, string value)
        {
            container.Add(key, value);
        }

        static void Main(string[] args)
        {
            SortedList s1 = new SortedList();
            Hashtable  h1 = new Hashtable();

            AddElement(s1, "001", "Zara Ali");
            AddElement(h1, "001", "Zara Ali");
        }
    }
}

或创建扩展方法:

public static class MyExtensions
{
    public static void AddElement(this IDictionary container, string key, string value)
    {
        container.Add(key, value);
    }
}
使用方法:

SortedList s1 = new SortedList();
Hashtable h1 = new Hashtable();

s1.AddElement("001", "Zara Ali");
h1.AddElement("001", "Zara Ali");

或创建扩展方法:

public static class MyExtensions
{
    public static void AddElement(this IDictionary container, string key, string value)
    {
        container.Add(key, value);
    }
}
使用方法:

SortedList s1 = new SortedList();
Hashtable h1 = new Hashtable();

s1.AddElement("001", "Zara Ali");
h1.AddElement("001", "Zara Ali");

IEnumerable
也不包含
Add
。您需要非泛型的
IDictionary
IEnumerable
也不包含
Add
。你需要非泛型<代码> IDICORIGION/COMPE >。在一个无关的注释中,使用“<代码> REF <代码>是不必要的,因为你不重新分配变量。@ PikFuLydx33:对不起,我来自C++,它是我第一个使用C语言的进程;但真的,有什么意义?如果它有帮助(而不是使用已经定义的
Add()
方法)?@SeM在我的例子中,它不需要有帮助,我只是尝试操作泛型和集合。“定义的Advices Works:”,在一个不相关的注释中,使用“<代码> REF < /C>”是不必要的,因为您不重新分配变量。@ PikFuLyDX33:对不起,我来自C++,它是我第一个使用C*的PROG;但真的,有什么意义?如果它有帮助(而不是使用已经定义的
Add()
方法)?@SeM在我的例子中,它不需要有帮助,我只是尝试操作泛型和集合。定义的Add works:)。Yeah make sens:)我试图用集合操纵一些泛型,但不知道IDictionary。Yeah make sens:)我试图用集合操纵一些泛型,但不知道IDictionary。尼斯,喜欢扩展方法的这个概念尼斯,喜欢扩展方法的这个概念