https://github.com/karenpayneoregon/indices-ranges-csharp
Simple indices and range code samples
https://github.com/karenpayneoregon/indices-ranges-csharp
csharpcore
Last synced: about 1 month ago
JSON representation
Simple indices and range code samples
- Host: GitHub
- URL: https://github.com/karenpayneoregon/indices-ranges-csharp
- Owner: karenpayneoregon
- Created: 2023-10-22T21:02:15.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-23T17:08:20.000Z (over 2 years ago)
- Last Synced: 2025-01-29T00:28:50.512Z (over 1 year ago)
- Topics: csharpcore
- Language: C#
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# About
Code samples for [Indices and ranges](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/ranges-indexes). More samples to follow.

Example code for above
```csharp
private static void MonthsIndexing()
{
Print();
List> monthContainer = RangeDetails(MonthNames());
foreach (var item in monthContainer)
{
Console.WriteLine($"{item.Value,-10}{item.Index,-4}{item.EndIndex}");
}
}
private static void PersonIndexing()
{
Print();
List people = new()
{
new() { Id = 1, FirstName = "Jim", LastName = "Gallagher"},
new () { Id = 2, FirstName = "Clare", LastName = "Gallagher"},
new () { Id = 3, FirstName = "Mary", LastName = "Gallagher"}
};
var personContainer = RangeDetails(people);
Console.WriteLine($"Id Person Index Start End");
foreach (var item in personContainer)
{
Console.WriteLine($"{item.Value,-10} {item.Index,-4} {item.StartIndex,3} {item.EndIndex,7}");
}
}
```
Class to get indices
```csharp
class Helpers
{
public static List> RangeDetails(List list) =>
list.Select((element, index) => new ElementContainer
{
Value = element,
StartIndex = new Index(index),
EndIndex = new Index(Enumerable.Range(0, list.Count).Reverse().ToList()[index], true),
Index = index + 1
}).ToList();
}
```
Model
```csharp
public class ElementContainer
{
public T Value { get; set; }
public Index StartIndex { get; set; }
public Index EndIndex { get; set; }
public int Index { get; set; }
public override string ToString() => $"{Index,-5:D2}{Value}";
}
```