在我的ArrayList中添加,更改和删除? [英] add, change and delete in my ArrayList?
本文介绍了在我的ArrayList中添加,更改和删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在尝试在c#中进行预订程序.
我需要帮助的是如何编写方法来添加,更改和删除ArrayList中的客户对象?
到目前为止,我已经做到了,但是我认为这是不正确的
Hi, Im trying to make a reservation program in c #.
What I need help with is how I can write methods to add, change and delete customer object in my ArrayList?
i have done this so far but it isnt correct in my believ
class CustomerManager
{
private ArrayList customers;
public CustomerManager()
{
ArrayList cust = new ArrayList();
customers = cust;
}
public int Count
{
get
{
return Count;
}
set
{
foreach (int i in customers)
{
Count++;
}
}
}
public int GetNewID
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
public bool AddCustomer()
{
foreach(int i in customers)
if (customers != null)
{
customers.Add(i);
return true;
}
else
return false;
}
public bool ChangeCustomer()
{
throw new System.NotImplementedException();
}
public bool DeleteCustomer()
{
throw new System.NotImplementedException();
}
public Customer GetCustomer()
{
throw new System.NotImplementedException();
}
public void TestValues()
{
throw new System.NotImplementedException();
}
}
}
推荐答案
首先,不要使用ArrayList
.此类型是非泛型的.通过在.NET Framework 2.0中引入泛型,使所有此类集合类型都已过时.非泛型集合不好,因为它们需要类型转换,这可能是不安全的.
始终使用通用集合类,例如System.Collections.Generic.List
,请参见:
http://msdn.microsoft.com/en-us/library/system.collections. generic.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx [ ^ ].
即使ArrayList
和其他非泛型集合由于它们足以使旧项目兼容而没有正式过时,但对于任何新开发来说,它们都是过时的.
进行更改;而其他一切都是微不足道的.
—SA
First of all, don''t useArrayList
. This type is non-generic. All such collection types are made obsolete by introduction of generics in .NET Framework 2.0. The non-generic collections are not good because they require type cast which is potentially unsafe.
Always use generic collection classes such asSystem.Collections.Generic.List
, see:
http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^],
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^].
Even thoughArrayList
and other non-generic collections are not formally made obsolete because they are good enough to keep legacy projects compatible, they are obsolete for any new development.
Make this change; and everything else is trivial.
—SA
这篇关于在我的ArrayList中添加,更改和删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文