Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created January 30, 2024 00:17
Show Gist options
  • Save karenpayneoregon/e823f866a47162c34523683c1131c0e8 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/e823f866a47162c34523683c1131c0e8 to your computer and use it in GitHub Desktop.
Provides code to show start and end index for each element in a list

This code works but it can be improved.

public class Container<T>
{
public T? Value { get; set; }
public Index StartIndex { get; set; }
public Index EndIndex { get; set; }
}
var Months = DateTimeFormatInfo.CurrentInfo.MonthNames[..^1].ToList();
var containers = Months.Get();
foreach (var container in containers)
{
Debug.WriteLine($"{container.StartIndex,-4}{container.EndIndex,-4}{container.Value}");
}
public static class RangeHelpers
{
public static List<Container<T>> Get<T>(this List<T> list) =>
list.Select((element, index) => new Container<T>
{
Value = element,
StartIndex = new Index(index),
EndIndex = new Index(Enumerable.Range(0, list.Count).Reverse().ToList()[index], true)
}).ToList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment