473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to delete items from list using foreach

1
Expand|Select|Wrap|Line Numbers
  1. if (r == "delete"){
  2. Console.Write("Who do you want to remove from the list?: ");
  3. int x = Convert.ToInt32(Console.ReadLine());
  4. foreach (var person in Person) {
  5. if (x == person.Id) Person.RemoveAt(x);
  6. }
  7. }
Aug 12 '19 #1
2 2628
dev7060
636 Expert 512MB
- foreach loop iterates through elements but for performing deletion using RemoveAt(), index must be known.

- for loop can be used with RemoveAt() for such purpose. Here's the pseudo code/algo:
Expand|Select|Wrap|Line Numbers
  1.  IF CHOICE == 'DELETE' 
  2.    x = What to delete
  3.    for (int i = 0; i < list.Count; i++){
  4.     if( x == list[i]){
  5.        list.RemoveAt[i];
  6.     }
  7.    }
  8.  
- Index can also be obtained using IndexOf method, but it returns the first index of an item if found in the list.
Aug 13 '19 #2
You can attempt this code, and I hope it helps you.
Expand|Select|Wrap|Line Numbers
  1. var list = new List<int>(Enumerable.Range(1, 10));
  2. for (int i = list.Count - 1; i >= 0; i--)
  3. {
  4.     if (list[i] > 5)
  5.         list.RemoveAt(i);
  6. }
  7. list.ForEach(i => Console.WriteLine(i));
thank you.
Aug 19 '19 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Colin Steadman | last post by:
I have a multi-dimensional array that I want to delete items from. To do this I display a form and the user clicks some tick boxes. Then on the delete page I check which tick boxes were...
3
by: Filippo P. | last post by:
Hi there, I have a menu (Collection) that needs to be trimmed based on security access of the logged user. protected void AdjustMenuBasedOnUserSecurity(Items ItemsList) { foreach (Item i in...
2
by: tomcarr1 | last post by:
In this walkthrough on "Allowing Users to Delete Items in a DataGrid Web Server Control" at: ...
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
8
by: Brian L. Troutwine | last post by:
I've got a problem that I can't seem to get my head around and hoped somebody might help me out a bit: I've got a dictionary, A, that is arbitarily large and may contains ints, None and more...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
3
by: Doug | last post by:
I have a generic list object with a property called, "MarkedForDeletion". During the course of my processing, some of the objects in the list will get this property set to true and so at the end...
1
by: ramya naidu | last post by:
Hai iam working on a project where i use generic list to add the items and when diaplaying the items i can dispaly first and last item of the list using first and last buttons but when i press next...
2
by: =?Utf-8?B?RHJEQkY=?= | last post by:
I understand that the Value put into a DataGridViewComboBoxCell has to be a member of the Items list or an exception is thrown. I also understand that you can override that exception by handling...
0
by: Bharthish k v | last post by:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.