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 901635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:35:13+00:00 2026-05-15T15:35:13+00:00

Take List<Car> . Each Car has a unique index identifying it, say RegNumber and

  • 0

Take List<Car>. Each Car has a unique index identifying it, say RegNumber and then another property describing it – say Color for example.

I want to

  1. check if the collection has a car with RegNumber 5
  2. if it does, change the color
  3. if it doesn’t, add a new item for that car
  4. save the list

This is the way I am currently doing it and I’m asking if there is a better, more efficient way of doing this?

Car car = CarsForSale.Find(c => c.RegNumber == 5);

if (car != null)
{
   foreach (Car car in CarsForSale)
   {
      if (car.RegNumber == 5)
      {
         car.Color = "Red";
         break;
      }
   }
}
else
{
   CarsForSale.Add(new Car(5, "Red"));
}

Save(CarsForSale);

EDIT
There are not multiple cars with the same reg – the RegNumber is unique as stated in the question.

This was really just a total dumb@ss moment here anyway that a code review would have spotted. Thanks for all the replies and for not mocking my clearly stoopid question. Of course the item/element returned from the collection is a reference so there is absolutely no need to iterate through the list again…time to go bang my head against a wall I think.

  • 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-15T15:35:13+00:00Added an answer on May 15, 2026 at 3:35 pm

    Well first off you don’t need your test for car.RegNumber == 5 in the loop – you’ve already found the first car that match this criterion from your statement:

    Car car = CarsForSale.Find(c => c.RegNumber == 5);
    

    In fact your whole loop is redundant, you can just have:

    if (car != null)
    {
        car.Color = "Red";
    }
    else
    {
        CarsForSale.Add(new Car(5, "Red"));
    }
    

    Unless you want to find all cars that have RegNumber equal to 5, in which case your first line is incorrect as that will only find the first car that matches the criteria. To find all the cars you want something along these lines:

    var cars = CarsForSale.Where(c => c.RegNumber == 5);
    
    foreach (Car car in cars)
    {
        car.Color = "Red";
    }
    
    if (!car.Any())
    {
        CarsForSale.Add(new Car(5, "Red"));
    }
    

    With your original code the compiler should have warned you that the redefinition of car in the loop would hide the original definition (the one I’ve quoted).

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

Sidebar

Related Questions

The goal here is to take some list of coordinates, like [[1,2],[3,4],[7,1]] , then
i want to take a list of anonymous types and export then to excel.
Is there some Haskell function that can take a list, say of doubles, like
Take a list of zoo documents in ravendb, each of which have a field
I would like to take my list of tuples, where each tuple consists of
I would like to take a list of RawDoc which each have a title
I want to take a list [0,1,2] and turn it into [0,1,2,2,1,0] . Right
Okay one of my homework problems is to take a list of lists and
I am trying to write a simple Java function that will take a list
In Haskell, there is a function take n list which returns the first n

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.