Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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# 声明泛型集合_C#_Generics - Fatal编程技术网

C# 声明泛型集合

C# 声明泛型集合,c#,generics,C#,Generics,我有3个类(A,B,C),并且必须为所有类实现一个存储方法,因此我考虑使用一个通用列表,如list=newlist()但它不允许我使用它 我希望方法如下: class Bascket { List<T> list= new List<T>(); public void addToBasket(T value) { list.Add(value); } } var basket = new Basket<string>()

我有3个类(
A
B
C
),并且必须为所有类实现一个存储方法,因此我考虑使用一个通用列表,如
list=newlist()但它不允许我使用它

我希望方法如下:

class Bascket
{
   List<T> list= new List<T>();

   public void addToBasket(T value)
   {
      list.Add(value);
   }
}
var basket = new Basket<string>();
basket.addToBasket("foo"); // OK
basket.addToBasket(1); // Fail, int !== string
Bascket类
{
列表=新列表();
公共void addToBasket(T值)
{
列表。添加(值);
}
}

问题在于未声明
T
。您可以将泛型参数添加到类中,以使其正常工作:

class Basket<T>
{
   List<T> list= new List<T>();

   public void addToBasket(T value)
   {
      list.Add(value);
   }
}
类篮
{
列表=新列表();
公共void addToBasket(T值)
{
列表。添加(值);
}
}
这允许您像这样使用类:

class Bascket
{
   List<T> list= new List<T>();

   public void addToBasket(T value)
   {
      list.Add(value);
   }
}
var basket = new Basket<string>();
basket.addToBasket("foo"); // OK
basket.addToBasket(1); // Fail, int !== string
var basket=新basket();
basket.addToBasket(“foo”);//好啊
篮子。addToBasket(1);//失败,int!==一串

问题在于未声明
T
。您可以将泛型参数添加到类中,以使其正常工作:

class Basket<T>
{
   List<T> list= new List<T>();

   public void addToBasket(T value)
   {
      list.Add(value);
   }
}
类篮
{
列表=新列表();
公共void addToBasket(T值)
{
列表。添加(值);
}
}
这允许您像这样使用类:

class Bascket
{
   List<T> list= new List<T>();

   public void addToBasket(T value)
   {
      list.Add(value);
   }
}
var basket = new Basket<string>();
basket.addToBasket("foo"); // OK
basket.addToBasket(1); // Fail, int !== string
var basket=新basket();
basket.addToBasket(“foo”);//好啊
篮子。addToBasket(1);//失败,int!==一串

假设A、B和C是您希望存储在篮子对象中的项,您应该创建这些项的基类,并将泛型集合声明为基类集合,即

public interface IBasketItem
{ 
    /* put some common properties and methods here */
    public decimal Price { get; set; }
    public string Name { get; set; }
}
public class A : IBasketItem
{ /* A fields */ }
public class B : IBasketItem
{ /* B fields */ }
public class C : IBasketItem
{ /* C fields */ }

public class Basket
{
    private List<IBasketItem> _items = new List<IBasketItem>();

    public void Add(IBasketItem item)
    {
        _items.Add(item);
    }

    public IBasketItem Get(string name)
    {
        // find and return an item
    }
}
然而,当检索回项时,您应该使用公共接口,或者您应该知道对象实际上是哪种类型的。例如:

IBasketItem myItem = basket.Get("cheese");
Console.WriteLine(myItem.Name);
// Take care, if you can't be 100% sure of which type returned item will be
// don't cast. If you cast to a wrong type, your application will crash.
A myOtherItem = (A)basket.Get("milk");
Console.WriteLine(myOtherItem.ExpiryDate);

假设A、B和C是您希望存储在Basket对象中的项,您应该创建这些项的基类,并将泛型集合声明为基类集合,即

public interface IBasketItem
{ 
    /* put some common properties and methods here */
    public decimal Price { get; set; }
    public string Name { get; set; }
}
public class A : IBasketItem
{ /* A fields */ }
public class B : IBasketItem
{ /* B fields */ }
public class C : IBasketItem
{ /* C fields */ }

public class Basket
{
    private List<IBasketItem> _items = new List<IBasketItem>();

    public void Add(IBasketItem item)
    {
        _items.Add(item);
    }

    public IBasketItem Get(string name)
    {
        // find and return an item
    }
}
然而,当检索回项时,您应该使用公共接口,或者您应该知道对象实际上是哪种类型的。例如:

IBasketItem myItem = basket.Get("cheese");
Console.WriteLine(myItem.Name);
// Take care, if you can't be 100% sure of which type returned item will be
// don't cast. If you cast to a wrong type, your application will crash.
A myOtherItem = (A)basket.Get("milk");
Console.WriteLine(myOtherItem.ExpiryDate);


但它并没有达到我想要的效果,因为basket只能接受A类而不能接受B类或者Clike var basket=new basket();addToBasket(A)但不能将B和C添加到同一个列表A、B、C应实现接口Iabc,并且应使用it@XandrUu您刚才发布的代码将与alexn的答案一起使用。@XandrUu如果您想多次接受,请给他们一个通用接口,然后给你的篮子添加一个通用约束:
class basket,其中T:IFace
。但它不能满足我的要求,因为篮子只能接受类a而不能接受类B,或者Clike var basket=new basket();addToBasket(A)但不能将B和C添加到同一个列表A、B、C应实现接口Iabc,并且应使用it@XandrUu您刚才发布的代码将与alexn的答案一起使用。@XandrUu如果您想多次接受,请给他们一个通用接口,然后将一个通用约束添加到您的basket:
类basket,其中T:IFace
。这应该是正确的方法,但是接口中有6个属性fildes,a有4个,B有4个,C有4个。如果我实现一个接口,我必须使用该接口中声明的所有6个属性,我不知道该接口有6个属性的方法?创建您自己的界面,只使用您需要的东西,如上面的示例所示。您尝试使用哪个接口?我的自定义接口IBasketItem有6个属性,类A只使用4,类B使用4,类C使用4,如果我只在4个属性上实现,它会给我类似“error not implementation interface member”的错误,那么这是用于其他对象的接口。创建接口IItem,它将只包含A、B和C共有的项。如果它们没有共同点,则创建一个没有成员的接口,并从A、B和C类继承它。在这种情况下,使用一个没有属性的接口。这称为[marker interface](),用于类似的情况。这应该是正确的方法,但接口中有6个属性fildes,a有4个,B有4个,C有4个。如果我实现一个接口,我必须使用该接口中声明的所有6个属性,我不知道该接口有6个属性的方法?创建您自己的界面,只使用您需要的东西,如上面的示例所示。您尝试使用哪个接口?我的自定义接口IBasketItem有6个属性,类A只使用4,类B使用4,类C使用4,如果我只在4个属性上实现,它会给我类似“error not implementation interface member”的错误,那么这是用于其他对象的接口。创建接口IItem,它将只包含A、B和C共有的项。如果它们没有共同点,则创建一个没有成员的接口,并从A、B和C类继承它。在这种情况下,使用一个没有属性的接口。这称为[marker interface](),用于类似的情况。