C# 我正在写一个通用的<;T>;数组的列表,我正在尝试从索引1中删除一个项,并使下一个项移位

C# 我正在写一个通用的<;T>;数组的列表,我正在尝试从索引1中删除一个项,并使下一个项移位,c#,C#,因此,当我移除时,它应该是这样的: myArray=衬衫、裤子、鞋子、帽子 NewArray=裤子、鞋子、帽子、空 我使用的方法是: public bool Remove(T items) { for(int i = 0; i < Count; i++) { if (items == array[i]) { array[i] = array[i + 1]; Count—-; } el

因此,当我移除时,它应该是这样的:

myArray=衬衫、裤子、鞋子、帽子

NewArray=裤子、鞋子、帽子、空

我使用的方法是:

public bool Remove(T items)
{
   for(int i = 0; i < Count; i++)
   {
       if (items == array[i])
       {
          array[i] = array[i + 1];
          Count—-;
       }
       else 
       {
          Return False;
       }
   }
}
public bool Remove(T项)
{
for(int i=0;i
但是我得到了一个错误代码

运算符==不能应用于T和T类型的操作数


我试图将运算符改为just=但随后又出现另一个错误,无法将T转换为bool。我在谷歌上搜索过,找不到解决方案。

对于您试图做的事情,我有很多不确定的地方:

但是如果我假设您想要一个泛型类,那么类似这样的东西应该可以工作:

public static T[] RemoveFirst(T[] items)
{
    T[] result = new T[items.Count() - 1];      

    for (int i = 1; i < items.Count(); i++)
    {           
        result[i - 1] = items[i];
    }

    return result;
}
public static T[] RemoveAt(T[] items, int X)
{
    T[] result = new T[items.Count() - 1];      

    int indexOfLastResult = 0;
    for (int i = 0; i < items.Count(); i++)
    {
        if (i != X)
        {
            result[indexOfLastResult] = items[i];               
            indexOfLastResult++;
        }
    }

    return result;
}
你想让这个比较做什么?因为你现在说:

If thisArray == thisParticularInstanceInTheArray
这永远是错误的<代码>T!=T[i]
没有意义

另外一个想法是,也许您正在尝试使用RemoveAt(intx)方法?而不是先拆

如果是这样的话:这种比较应该更像这样:

public static T[] RemoveFirst(T[] items)
{
    T[] result = new T[items.Count() - 1];      

    for (int i = 1; i < items.Count(); i++)
    {           
        result[i - 1] = items[i];
    }

    return result;
}
public static T[] RemoveAt(T[] items, int X)
{
    T[] result = new T[items.Count() - 1];      

    int indexOfLastResult = 0;
    for (int i = 0; i < items.Count(); i++)
    {
        if (i != X)
        {
            result[indexOfLastResult] = items[i];               
            indexOfLastResult++;
        }
    }

    return result;
}
publicstatict[]RemoveAt(T[]items,intx)
{
T[]结果=新的T[items.Count()-1];
int indexOfLastResult=0;
对于(int i=0;i
您告诉编译器
项可以是任何东西,但不能假设
==
可以处理“任何东西”。所以不要那样做。什么类型是myArray
?您需要说
public bool Remove(WhateverTypeMyArrayIs items)
您不希望数组没有空值吗?如果该值出现多次,该怎么办?如果它出现在
0
以外的索引中,该怎么办?(顺便说一句,你在问题中提到了索引
1
。我想你指的是索引
0
)。你应该使用.Equals()或.ReferenceEquals()。但是您的“array[i]=array[i+1];”是不明确的:您想做什么?因为您有可能与您的案例相匹配的列表或其他预定义类。请共享您的类的完整代码。可能有重复的