简体   繁体   English

C#列表<T>自动递增

[英]C# List<T> Auto Increment

When I create a class that I have created and I create two lists with the new operator, the ID number is in the order of both when I start adding data.当我创建一个已创建的类并使用 new 运算符创建两个列表时,ID 号在我开始添加数据时按两者的顺序排列。

After adding 10 pieces to the list 1, when I start adding 10 to List 2, ID numbers 11, 12, 13 starts to give.在列表 1 中添加 10 件后,当我开始向列表 2 添加 10 时,ID 号 11、12、13 开始给出。

Every list I want is a different way to increase the IDs.我想要的每个列表都是增加 ID 的不同方式。

If you can help, good work.如果你能帮上忙,那就干得好。

Sample Class样本类

public class Listem
{
    private static int xid;
    public int ID;

    public DateTime Date;

    public byte Week;

    public char Process;
    public string PerName;

    public float Pips;


    public Listem(string PerName, char LongShort, byte Hafta, float Pips)
    {
        xid++;
        this.ID = xid;
        this.PerName = PerName;
        this.Process = LongShort;
        this.Week = Hafta;

        this.Pips = Pips;
    }


    public override string ToString()
    {
        return $"ID : {this.ID}\tPerName : {this.PerName}   Tarih : {String.Format("{0:yyyy-MM-dd}", this.Date)}\tİşlem Yönü :    {this.Process}\tHafta :     {this.Week}\tPips :     {this.Pips}";
    }
}

Main Program主程序

    static void Main(string[] args)
    {
        List<Listem> Liste_1 = new List<Listem>();
        List<Listem> Liste_2 = new List<Listem>();

        for (int i = 0; i < 10; i++)
        {
            Liste_1.Add(new Listem("Ahmet", '2', 2, 9F));
        }
        for (int i = 0; i < 10; i++)
        {
            Liste_2.Add(new Listem("Mehmet", '3', 4, 19F));
        }
        Console.WriteLine("Liste 1  ===============");
        foreach (var item in Liste_1)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine("Liste 2  ===============");
        foreach (var item in Liste_2)
        {
            Console.WriteLine(item);
        }

    }

Result结果

enter image description here在此处输入图片说明

To use your existing code and get the ID's to reset, you could provide a method to allow the clients to reset the static field xid on the Listem class back to 0 before adding items.要使用现有代码并重置 ID,您可以提供一种方法,允许客户端在添加项目之前将Listem类上的static字段xid重置回0 This is not a great design, however, because it requires that the client's know they have to call this method before creating a new list.然而,这不是一个很好的设计,因为它要求客户端知道他们必须在创建新列表之前调用此方法。

public class Listem
{
    private static int xid;

    // [Other class code here]

    public static void ResetId()
    {
        xid = 0;
    }
}

If you want to allow the users to control the ID property, then you should probably just make it another public property of the class.如果您想允许用户控制ID属性,那么您可能应该将其设为类的另一个公共属性。 Oh, and for that matter, you aren't using properties, you're using public fields, which is also considered a bad design.哦,就此而言,您没有使用属性,而是使用公共字段,这也被认为是一种糟糕的设计。 For public members, always opt for properties.对于公共成员,始终选择属性。

This is the quickest way to get what you want, though:不过,这是获得所需内容的最快方法:

// Before populating a new list, always reset the xid field to '0'
Listem.ResetId();

for (int i = 0; i < 10; i++)
{
    Liste_1.Add(new Listem("Ahmet", '2', 2, 9F));
}

// Before populating a new list, always reset the xid field to '0'
Listem.ResetId();

for (int i = 0; i < 10; i++)
{
    Liste_2.Add(new Listem("Mehmet", '3', 4, 19F));
}

However a better way to do this would be to make all your public fields properties, including ID , and get rid of the static field:但是,更好的方法是使所有公共字段属性,包括ID ,并摆脱静态字段:

public class Listem
{
    public int ID { get; set; }
    public DateTime Date { get; set; }
    public byte Week { get; set; }
    public char Process { get; set; }
    public string PerName { get; set; }
    public float Pips { get; set; }

    public Listem(int id, string perName, char longShort, byte hafta, float pips)
    {
        ID = id;
        PerName = perName;
        Process = longShort;
        Week = hafta;
        Pips = pips;
    }

    public override string ToString()
    {
        return $"ID : {ID}\tPerName : {PerName} Tarih : {Date:yyyy-MM-dd}" + 
            $"\tİşlem Yönü : {Process}\tHafta : {Week}\tPips :  {Pips}";
    }
}

Then you can just use the for loop iterator as the ID when creating your lists:然后,您可以在创建列表时使用for循环迭代器作为ID

for (int i = 0; i < 10; i++)
{
    Liste_1.Add(new Listem(i, "Ahmet", '2', 2, 9F));
}

for (int i = 0; i < 10; i++)
{
    Liste_2.Add(new Listem(i, "Mehmet", '3', 4, 19F));
}

Then to output the lists, it would look like:然后输出列表,它看起来像:

Liste_1.ForEach(Console.WriteLine);
Console.WriteLine(new string('-', Console.WindowWidth));

Liste_2.ForEach(Console.WriteLine);
Console.WriteLine(new string('-', Console.WindowWidth));

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM