Timeline for Update first and last item in List<T>
Current License: CC BY-SA 3.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 26, 2014 at 19:47 | comment | added | Simon Forsberg | @RuneFS You're right, if null values are allowed in the list then it makes things more complicated. I assume that there are no null values in the list. | |
Jun 26, 2014 at 19:45 | comment | added | Rune FS | your first example will fail if either the first or the last element in the list is null and that that null value is not the only null value in the list. E.g. if both the first and the last element are null then they will both have first and last set to true regardless of how many elements there's in the list | |
Jun 26, 2014 at 17:00 | comment | added | Simon Forsberg |
@Donniewiko You don't need to separate the two if s in your solution. If a list has a first item, it also has a last item. Thanks for the edit, @ANeves.
|
|
Jun 26, 2014 at 16:56 | comment | added | ANeves | @SimonAndréForsberg I edited the code to remove the bug of a possible NullReferenceException. | |
Jun 26, 2014 at 16:54 | history | edited | ANeves | CC BY-SA 3.0 |
Removed possibility of NullReferenceException.
|
Jun 26, 2014 at 16:39 | comment | added | Simon Forsberg |
@Donniewiko result.FirstOrDefault().First = true; result.LastOrDefault().Last = true; will work fine when there's only one item in the list, yes. Although based on your edit, I think you can do it differently (cleaner) without those properties on each item.
|
|
Jun 26, 2014 at 16:32 | vote | accept | Donniewiko | ||
Jun 26, 2014 at 16:00 | comment | added | Donniewiko | Thanks for your reply. The result is already a .ToList() so the FirstOrDefault and LastOrDefault should be on the List that sits in memory. And about your second reply, will that work on when there is one item in the list? I need the property to be true for both properties then. (it obviously throws an error when list is null) | |
Jun 26, 2014 at 15:57 | history | answered | Simon Forsberg | CC BY-SA 3.0 |