.NET list access index [duplicate] - c#

This question already has answers here:
Is the order of elements on a C# List<T> deterministic?
(3 answers)
Closed 8 years ago.
Follow up question from How to select List<> by its index and return the content?
Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?
List<string> pizzas = new List<string>();
pizzas.Add("Angus Steakhouse");
pizzas.Add("Belly Buster");
pizzas.Add("Pizza Bianca");
pizzas.Add("Classic Cheese");
pizzas.Add("Friday Special");
string result = pizzas[1]; // result is equal to "Belly Buster"

index starts at 0 so yes if the code stays the same..belly buster will always be at 1

Related

How do I delete a specific list of lines from a text document? [duplicate]

This question already has answers here:
How can I efficiently remove elements by index from a very large list?
(6 answers)
Remove oldest n Items from List using C#
(6 answers)
Remove all but the first item in a list
(2 answers)
How to remove selected array values in C#
(5 answers)
Move First 10 List Items to Another Item List
(5 answers)
Closed 1 year ago.
This is what I have but there isn't anything that talks about how to delete multiple specific lines, for example, from line 2 - 8.
var file = new List<string>(System.IO.File.ReadAllLines(lsbMatches.SelectedItem.ToString()));//location
file.RemoveAt(2);// i want to change this to multiple lines
File.WriteAllLines(lsbMatches.SelectedItem.ToString(), file.ToArray());//location
You can use RemoveRange because you have converted your text file into a list of strings.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removerange?view=net-5.0
You're best option is to do :
file.RemoveRange(2, 6);

Transfer content of one array to another, removing duplicates [duplicate]

This question already has answers here:
How do I remove duplicates from a C# array?
(28 answers)
Most efficient way to remove duplicates from a List
(1 answer)
Closed 6 years ago.
I have an array of about 1000 words. I want to transfer them to another array while removing any duplicates. How can I do this in C# ?
The question almost is the answer:
var array2 = array1.Distinct().ToArray();

C# Check if any int in list matches any int in another list [duplicate]

This question already has answers here:
How to find if an element of a list is in another list?
(5 answers)
Closed 9 years ago.
I apologize if this is an obvious question, but I cannot find the answer.
Say I have the following:
var list1 = new List<int>{1,2,3};
var list2 = new List<int>{3,5,6};
How can I see if ANY element of list1 is contained in list2? So in this case I want to return true because 3 is in both.
Performing nested loops will not work for me, so it would be ideal if there was a:
list1.HasElementIn(list2);
Use Enumerable.Intersect - it produces intersection of both sequences. If intersection is not empty, then there some item which exists in both sequences:
bool isAnyItemInBothLists = list1.Intersect(list2).Any();
One thing to note - thus Intersect is a deferred streaming operator, then you will get result as soon as any common item will be found. So, you don't need to wait until complete intersection will be computed.

C# Linq Query to get between items [duplicate]

This question already has answers here:
Get previous and next item in a IEnumerable using LINQ
(11 answers)
Closed 9 years ago.
Hi I have a dictionary like this
("ABC","X")
("CDE","C")
("EFG","X")
I need to retrieve the items which are in two sides of "C" , How to achieve this with linq ?
Output Expected :
Item = n / output = (n-1)(n+1) items
("ABC","X") and ("EFG","X")
Something like this?
Dict.TakeWhile(e => e.Value = "C").Take(1).Reverse().Take(3).
Then remove the middle one.

C# remove elements in list starting with # [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to remove elements from an array
I have an List with a bunch of data in it. Some of the lines start with a #. I want to remove those lines.
How...?
assuming its a string List
myList.RemoveAll(x => x.BeginsWith("#"));

Categories

Resources