Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1037783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:53:34+00:00 2026-05-16T14:53:34+00:00

If I have a list of objects, for example List<Cake> , is it possible

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:53:34+00:00Added an answer on May 16, 2026 at 2:53 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.