自动递增C#容器ID

发布于 2024-09-16 08:54:11 字数 899 浏览 2 评论 0原文

如果我有一个对象列表,例如 List,是否可以在 Cake 对象中拥有一个 private int key 来存储自动递增值每次我添加到列表中时。

我想要这个的原因是这样我可以填充一个包含列表中所有“蛋糕”的组合框,并为每个元素提供一个唯一值,而无需我自己输入。

例如:

public class Cake
{
    private int _id;
    private string _name;

    public Cake(string name)
    {
        _name = name;
    }

    public int GetID()
    {
        return _id;
    }
}

List<Cake> myCakeList = new List<Cake>();

myCakeList.Add(new Cake("Sponge"));
myCakeList.Add(new Cake("Chocolate"));
myCakeList.Add(new Cake("Battenburg"));

myCakeList.ForEach(x => {Console.WriteLine(x.GetID());});

理想情况下,此代码将返回:

0000001
0000002
0000003

或(如果我想要一个随机 ID)

389hguhg907903
357fboib4969gj
fhgw90290682gg

您能否告知这是否可能(对于两种密钥类型)以及如何完成。另外,这对于我的问题是否是一个糟糕的解决方案。

预先非常感谢,

阿什利

If I have a list of objects, for example List<Cake>, is it possible to have a private int key in the Cake object that will store an auto-incremented value every time I add to the list.

My reason for wanting this is so that I can populate a combobox containing all the "Cakes" in my list with a unique value for each element that I don't have to input myself.

For example:

public class Cake
{
    private int _id;
    private string _name;

    public Cake(string name)
    {
        _name = name;
    }

    public int GetID()
    {
        return _id;
    }
}

List<Cake> myCakeList = new List<Cake>();

myCakeList.Add(new Cake("Sponge"));
myCakeList.Add(new Cake("Chocolate"));
myCakeList.Add(new Cake("Battenburg"));

myCakeList.ForEach(x => {Console.WriteLine(x.GetID());});

Ideally this code would return:

0000001
0000002
0000003

or (if I wanted a random id)

389hguhg907903
357fboib4969gj
fhgw90290682gg

Could you please advise if this is at all possible (for both key types) and how it can be done. Also, whether or not this is a terrible solution to my problem.

Many thanks in advance,

Ashley

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落墨 2024-09-23 08:54:11

每次创建 Cake 实例时,_id 都会自动增加

public class Cake
{
    private int _id;
    private string _name;
    private static LastID;
    public Cake(string name)
    {
        _name = name;
        _id = LastID++;
    }

    public int GetID()
    {
        return _id;
    }
}

here the _id gets auto increased everytime you create an instance of a Cake

public class Cake
{
    private int _id;
    private string _name;
    private static LastID;
    public Cake(string name)
    {
        _name = name;
        _id = LastID++;
    }

    public int GetID()
    {
        return _id;
    }
}
请止步禁区 2024-09-23 08:54:11

列表中的项目通过索引访问。为什么不直接使用列表中已有的索引呢?

List<Cake> myCakeList = new List<Cake>();

myCakeList.Add(new Cake("Sponge"));
myCakeList.Add(new Cake("Chocolate"));
myCakeList.Add(new Cake("Battenburg"));

foreach (var i in Enumerable.Range(0, myCakeList.Length)) {Console.WriteLine(i);}

Items in a list are accessed by index. Why not just use the index already in your list?

List<Cake> myCakeList = new List<Cake>();

myCakeList.Add(new Cake("Sponge"));
myCakeList.Add(new Cake("Chocolate"));
myCakeList.Add(new Cake("Battenburg"));

foreach (var i in Enumerable.Range(0, myCakeList.Length)) {Console.WriteLine(i);}
空宴 2024-09-23 08:54:11

我通常认为这种编码风格是一个坏主意。添加到列表时生成 ID 是一种副作用,并不总是预期的(或者甚至已知会发生)。有时副作用是必要的(实际上很多编程都依赖于它们),但它们也可能很棘手。例如,如果同一个 Cake 对象被添加到两个列表中怎么办?然后呢?类似的意外可能会导致代码难以调试。

Slantrop 的想法是在创建时为所有 Cake 对象赋予独特的想法,这并不坏,并且是一种非常常见的模式。但你真的需要它吗?您仍然可以使用 object.ReferenceEquals() 之类的方法来区分对象。也许您真正需要的是经过深思熟虑的 EqualsGetHashcode 覆盖?

I generally find this style of coding to be a bad idea. This ID generation on adding to a list is a side effect, and not always expected (or even known to be happening). Side effects are sometimes necessary (indeed a lot of programming relies on them), but they can be tricky too. For example, what if the same Cake object gets added to two lists? What then? Surprises like that can lead to code that is hard to debug.

Slantroph's idea of just giving all Cake objects a unique idea upon creation isn't bad, and is a pretty common pattern. But do you really need it? You can still tell objects apart with the likes of object.ReferenceEquals(). Perhaps what you really need is well thought out Equals and GetHashcode overrides?

伴我老 2024-09-23 08:54:11

听起来您正在尝试获取类似于 RDBMS 表中的自动递增 ID 的内容。

RDBMS 中需要它们的原因是通过存储一个表中的记录的 ID 来识别另一个表中的记录。

在 C# 中处理内存中的对象时,这不是必需的。您只需在一个对象上有一个字段,该字段保存对另一个对象的引用。

RDBMS 表和 List之间的另一个重要区别是给定记录与单个表相关联。这不是对象在 C# 中的工作方式。它们都只是漂浮在记忆的“某个地方”。 List 只是一个保存对其他对象的引用的对象。因此其他列表(实际上还有其他类型的对象)可能保存对同一对象的引用。在语言层面上,没有人是另一个人的“所有者”。

It sounds like you're trying to get something akin to an auto-incrementing ID as found in RDBMS tables.

The reason they are needed in an RDBMS is to allow a record in one table to identify a record in another table, by storing the ID of the other record.

This is not necessary in C# when dealing with objects in memory. You just have a field in on one object that holds a reference to another object.

Another important difference between RDBMS tables and List<T> is that a given record is associated with a single table. That's not how objects work in C#. They are all just floating "somewhere" in memory. A List<T> is merely an object that holds references to other objects. So other lists (and indeed other types of object altogether) may be holding references to the same object. No one is the "owner" of another at the language level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文