在c#中创建自己的集合类, [英] Creating an own collection class in c#

查看:308
本文介绍了在c#中创建自己的集合类,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的c#开发人员,并在c#的学习曲线上。现在,我有一个非常小的项目,我应该编写自己的通用列表类存储数据。不允许使用任何.NET集合类。我的类应该有从列表中添加/删除项目和属性数量的方法。因为im不允许使用任何.NET colection类,我的列表应该存储在我的类中的数组。

I am a new c# developer and im on the learning curve on c#. Now, i have an very small project where I should write my own generic list class for storing data. Im not allowed to use any of the .NET collection classes. My class should have methods for adding/removing items and properties for the number of items from the list. Since im not allowed to use any of the .NET colection classes, my list should be stored in an array in my class.

那么,我到目前为止做了什么?因为我没有想法如何开始,我只是写下了方法,将用来得到一个愿景我应该做什么下一步:

So, what have I done so far? Since I have no Idea on how to start, I just wrote down the methods that will be used to get a vision of what I should do next:

public class myList
{
    public void addItem()
    {

    }

    public void removeItem()
    {

    }
}

推荐答案

我建议你阅读一下如何在MSDN上使用数组: http://msdn.microsoft.com/en-us/ library / aa288453(v = vs.71).aspx 。一般来说,要求社区基本上为你编写代码是不好的,但这里有一些指针:

I suggest that you read up on how to use arrays on MSDN: http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx. Generally it is bad form to ask the community to essentially write the code for you, but here are some pointers:


  • 编写一个构造函数来初始化该列表具有对象数组,例如 public myListClass(object [] items)

  • 您的addItem类应该传递您要添加的对象,因此: public void addItem(object newItem)

  • 您的removeItem应该有几个不同的重载,以便您可以确定要删除的内容。例如 remoteItem(int itemIndex) removeItem(string itemKey) removeItem / code>

  • 您可能希望将数组的当前大小存储为私有变量,并在每次添加项目时将其增加1。然后声明一个较大尺寸的新数组,并将原始项目复制到其中。

  • 您可能还需要一个排序方法,但这完全取决于将存储在数组中的对象的复杂程度。

  • 您可能不必在删除项目时减小数组的大小,但是您可能会发现有必要计数项目数等。如果每次都不减小数组的大小

  • 编写一个count()方法,返回数组中的项数 - 使用如果在删除项目时不减小数组的大小,则可以检查空值。

  • Write a constructor to initialize the list with an array of objects, e.g. public myListClass(object[] items)
  • Your addItem class should pass the object you are trying to add, so: public void addItem(object newItem)
  • Your removeItem should have a couple of different overloads so that you can determine what you want to remove. e.g. remoteItem(int itemIndex), removeItem(string itemKey) or removeItem(object item)
  • You will probably want to store the current size of the array as a private variable and increment this by 1 each time you add an item. You then declare a new array of the larger size and copy the original items into it. Copy the new item last.
  • You may also want a sort method, but this will depend entirely on how complex the objects are that will be stored in the array.
  • You probably don't have to reduce the size of the array when removing items though you may find it necessary to count the number of items etc. If you don't reduce the size of the array every time an item is removed be sure to take this into account when adding items (since it may already be large enough).
  • Write a count() method that returns the number of items in the array - use this to check for null values if you don't reduce the array's size when removing items.

还有很多其他注意事项,尤其是使用泛型允许存储多种类型。我建议您仔细阅读此主题: http://msdn.microsoft.com/ en-gb / library / 512aeb7t.aspx

There are a ton of other considerations, not least of which is using generics to allow multiple types to be stored. I suggest a little reading around the topic: http://msdn.microsoft.com/en-gb/library/512aeb7t.aspx

祝你好运!

这篇关于在c#中创建自己的集合类,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆