负责编写自定义扩展方法 .Add、.Remove 等
Tasked with writting cusom extension methods .Add, .Remove, etc
我的任务是编写一个通用的 class CustomList<T>
,其中应该包含通用的自定义扩展方法 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);
}
}
你已经完成一半了。您在 CustomList 中有一个名为 Add
的方法。您需要创建另一个名为 Remove
的方法。在删除中,您将删除从数组传递的值。在添加中,您只需将该值添加到列表中即可。
我在下面的 class 中添加了 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 class 并返回一个字符串可能会成功。
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
}
我确实更改了您的代码中的一些内容,并尽我所能评论每一行的作用。但是您的代码存在的一个问题是您正在创建一个新数组,该数组删除了所有以前的值,您可以在第 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;
}
}
我的任务是编写一个通用的 class CustomList<T>
,其中应该包含通用的自定义扩展方法 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);
}
}
你已经完成一半了。您在 CustomList 中有一个名为 Add
的方法。您需要创建另一个名为 Remove
的方法。在删除中,您将删除从数组传递的值。在添加中,您只需将该值添加到列表中即可。
我在下面的 class 中添加了 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 class 并返回一个字符串可能会成功。
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
}
我确实更改了您的代码中的一些内容,并尽我所能评论每一行的作用。但是您的代码存在的一个问题是您正在创建一个新数组,该数组删除了所有以前的值,您可以在第 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;
}
}