C# 负责编写cusom扩展方法。添加、删除等

C# 负责编写cusom扩展方法。添加、删除等,c#,generics,customization,add,extension-methods,C#,Generics,Customization,Add,Extension Methods,我的任务是编写一个泛型类CustomList,该类应使用泛型列表的自定义扩展方法Add、Remove和ToString。我对此感到非常困惑,到目前为止我所做的如下 public class CustomList<T> { private int count; public int Count { get { return count; } set { count = val

我的任务是编写一个泛型类
CustomList
,该类应使用泛型列表的自定义扩展方法
Add
Remove
ToString
。我对此感到非常困惑,到目前为止我所做的如下

    public class CustomList<T>
    {
        private int count;
        public int Count
        {
            get { return count; }
            set { count = value; }
        }
        public T[] Add(T value)
        {
            T[] myArray = new T[Count];
            myArray.Insert(0, value);
            return myArray;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            CustomList<int> list = new CustomList<int>();
            int value = 8;
            list.Add(value);
        }
    }
公共类自定义列表
{
私人整数计数;
公共整数计数
{
获取{返回计数;}
设置{count=value;}
}
公共T[]加(T值)
{
T[]myArray=新的T[计数];
myArray.Insert(0,值);
返回myArray;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
CustomList=新建CustomList();
整数值=8;
列表。添加(值);
}
}

你已经成功了一半。在CustomList中有一个名为
Add
的方法。您需要创建另一个名为
Remove
的方法。在remove中,您将删除从数组中传递的值。在Add中,您只需将该值添加到列表中

我在下面的类中添加了Remove方法

public class CustomList<T>
{
    private int count;
    public int Count
    {
        get { return count; }
        set { count = value; }
    }
    public T[] Add(T value)
    {// add value to array
        T[] myArray = new T[Count];
        myArray.Insert(0, value);
        return myArray;
    }
    public T[] Remove(T value)
    {
        //Find value in array and delete it
    }
}

我确实更改了代码中的一些内容,并尽我所能对每一行所做的事情进行了注释。但是代码中的一个问题是,您正在创建一个新数组,该数组删除了所有以前的值,您可以在第11行看到这一点。另外,我没有包括我认为是您创建的Insert函数。这将使您更好地了解数组的工作方式,并将成为您接下来必须编写的几个函数的良好起点。祝你好运

public class CustomList<T>
{
    private T[] MyArray { get; set; }
    // We don't want them being able to set the size of our array do we? 
    public int Count { get; private set; }


    public CustomList()
    {
        MyArray = new T[0];
        Count = 0;
    }

    // Add Value
    public T[] Add(T value)
    {
        // Because we are adding a item, add one to the count
        Count++;

        // Copy the old array so we do not lose any values
        var copyArray = MyArray;

        // Reinitialize the Array to the new size
        MyArray = new T[Count];

        // Iterate through each item and add it to the array
        for (var index = 0; index < copyArray.Length; index++)
        {
            MyArray[index] = copyArray[index];
        }

        // Subtract one from count because Arrays start at 0
        // Add the new value
        MyArray[Count - 1] = value;

        return MyArray;
    }
}
公共类自定义列表
{
私有T[]MyArray{get;set;}
//我们不希望他们能够设置数组的大小,是吗?
公共整数计数{get;私有集;}
公共客户列表()
{
MyArray=新的T[0];
计数=0;
}
//增值
公共T[]加(T值)
{
//因为我们正在添加一个项目,所以在计数中添加一个
计数++;
//复制旧数组,这样我们就不会丢失任何值
var copyArray=MyArray;
//将数组重新初始化为新大小
MyArray=新的T[计数];
//遍历每个项并将其添加到数组中
对于(var index=0;index
添加一些输入和所需的输出如果您确实需要编写,那么您需要首先了解它们是什么。不过,你的意思并不明显。谢谢你的建议!我已经为Remove和ToString设置了空方法,只是其中没有任何内容。我只展示了Add,因为这是我唯一开始写的,而且不起作用。请问为什么我被否决了?我没有理解实际的问题吗?解释得不透彻?谢谢我没有投反对票,这看起来很完美!谢谢@约翰努奇:我猜,否决票更多地与问题的质量有关。人们希望提问者澄清为什么问题集中在扩展方法上。而且,他可能会(也许会完全删除这个词)。谢谢@JohnV!你介意把它标为已回答吗?
public class CustomList<T>
{
    private T[] MyArray { get; set; }
    // We don't want them being able to set the size of our array do we? 
    public int Count { get; private set; }


    public CustomList()
    {
        MyArray = new T[0];
        Count = 0;
    }

    // Add Value
    public T[] Add(T value)
    {
        // Because we are adding a item, add one to the count
        Count++;

        // Copy the old array so we do not lose any values
        var copyArray = MyArray;

        // Reinitialize the Array to the new size
        MyArray = new T[Count];

        // Iterate through each item and add it to the array
        for (var index = 0; index < copyArray.Length; index++)
        {
            MyArray[index] = copyArray[index];
        }

        // Subtract one from count because Arrays start at 0
        // Add the new value
        MyArray[Count - 1] = value;

        return MyArray;
    }
}