在 C# 中创建和使用自定义 List<T>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16475423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating and using a custom List<T> in C#
提问by Makai
I am trying to use a customized List were I have added a few additional tools. I want to apply this list to a long list of customized classes that I have created. All of the classes have an ID number and some of the tools in the List use the ID.
如果我添加了一些其他工具,我正在尝试使用自定义列表。我想将此列表应用于我创建的一长串自定义类。所有的类都有一个 ID 号,列表中的一些工具使用这个 ID。
Here is a portion of my code that I am trying to use. I hope this help you understand my problem.
这是我尝试使用的代码的一部分。我希望这可以帮助您了解我的问题。
namespace Simple_Point_of _Sales_System
{
public class MyList<T> : List<T>
{
internal int SetID()
{
return this.Max(n => n.ID) + 1;
}
internal T Find(int ID)
{
return this.Find(n => n.ID == ID);
}
internal T Add(T n)
{
Read();
Add(n);
Write();
return n;
}
internal void Remove(int ID)
{
Read();
if (this.Exists(t => t.ID == ID)) RemoveAll(t => t.ID == ID);
else MessageBox.Show(GetType().Name + " " + ID + " does not exist.", "Missing Item", MessageBoxButtons.OK, MessageBoxIcon.Error);
Write();
}
internal void Edit(int ID, T n)
{
Read();
if (this.Exists(t => t.ID == ID)) this[FindIndex(t => t.ID == ID)] = n;
else MessageBox.Show(GetType().Name + " " + ID + " does not exist.", "Missing Item", MessageBoxButtons.OK, MessageBoxIcon.Error);
Write();
}
internal MyList<T> Read()
{
Clear();
StreamReader sr = new StreamReader(@"../../Files/" + GetType().Name + ".txt");
while (!sr.EndOfStream)
Add(new T().Set(sr.ReadLine()));
sr.Close();
return this;
}
internal void Write()
{
StreamWriter sw = new StreamWriter(@"../../Files/" + GetType().Name + ".txt");
foreach (T n in this)
sw.WriteLine(n.ToString());
sw.Close();
}
}
public class Customer
{
public int ID;
public string FirstName;
public string LastName;
}
public class Item
{
public int ID { get; set; }
public string Category { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
public class MyClass
{
MyList<Customer> Customers = new MyList<Customer>();
MyList<Item> Items = new MyList<Item>();
}
}
采纳答案by Tomas Jansson
I think your custom list needs to put on some constraints on the generic type to allow that. I would update your signature to something like
我认为您的自定义列表需要对泛型类型施加一些限制以允许这样做。我会把你的签名更新为类似的
public class MyList<T> : List<T> where T : IIdentity { .... }
Here I used IIdentityas the interface defining ID, but it could also be a class.
这里我用作IIdentity定义的接口ID,但它也可以是一个类。
To update your code I would do something like this:
要更新您的代码,我会执行以下操作:
public interface IIdentity
{
int ID { get; }
}
public class Customer : IIdentity
{
public int ID { get; set;}
public string FirstName;
public string LastName;
}
public class Item : IIdentity
{
public int ID { get; set; }
public string Category { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
I changed the ID in Customerto be a property instead of instance variable.
我将 ID 更改Customer为属性而不是实例变量。

