【问题标题】:Tasked with writting cusom extension methods .Add, .Remove, etc [closed]负责编写自定义扩展方法.Add、.Remove等[关闭]
【发布时间】:2018-06-15 14:15:49
【问题描述】:

我的任务是编写一个泛型类CustomList<T>,它应该用自定义扩展方法AddRemoveToString 填充一个泛型列表。我对此非常迷茫,到目前为止我所拥有的如下..

    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);
        }
    }

【问题讨论】:

  • 添加一些输入和所需的输出
  • 如果你真的要写C# extension methods,那么你需要先了解它们是什么。但是,您的意思并不明显。

标签: c# generics customization add extension-methods


【解决方案1】:

我确实更改了您的代码中的一些内容,并尽我所能评论每一行所做的事情。但是您的代码的一个问题是您正在创建一个新数组,该数组正在删除所有以前的值,您可以在第 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;
    }
}

【讨论】:

  • 请问为什么我被否决了?我错过了理解实际问题吗?解释不透彻?谢谢!
  • 我没有投反对票,这看起来很完美!谢谢!
  • @JohnUcci 我猜反对票更多地与问题的质量有关。人们希望提问者澄清为什么问题集中在扩展方法上。而且,他可能还没有(也许完全删除这个词)。
  • 谢谢@JohnV!您介意将其标记为已回答吗?
【解决方案2】:

你已经成功了一半。您在 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
    }
}

当涉及到 ToString 方法时,您会想要覆盖该方法。

将这样的内容添加到您的 CustomList 类并返回一个字符串可能会起到作用。

public override string ToString()
{
        // Convert List to string
       // you could try looping through your list and creating a string out of them.
       //return string you create
}

【讨论】:

  • 感谢您的建议!我确实已经设置了 Remove 和 ToString 的空方法,但其中没有任何内容。我只展示了 Add,因为它是我唯一开始编写的,并且无法正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 2011-04-03
相关资源
最近更新 更多