Created
February 3, 2025 14:37
-
-
Save davepcallan/3ef63cfb4aa912741d5c842d48a9b15e to your computer and use it in GitHub Desktop.
Sample to pop straight into SharpLab to see how the indices and range syntax introduced in C# 8 gets lowered
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class Program | |
{ | |
static void Main() | |
{ | |
var payments = new List<Payment> | |
{ | |
new Payment { Id = 1, Amount = 100.00m }, | |
new Payment { Id = 2, Amount = 150.50m }, | |
new Payment { Id = 3, Amount = 200.75m }, | |
new Payment { Id = 4, Amount = 50.25m }, | |
new Payment { Id = 5, Amount = 300.00m } | |
}; | |
} | |
static void OutputIndiceNormal(List<Payment> payments) | |
{ | |
var lastPayment = payments[payments.Count - 1]; | |
} | |
static void OutputIndiceSimplified(List<Payment> payments) | |
{ | |
var lastPayment = payments[^1]; | |
} | |
static void OutputRangeNormal(List<Payment> payments) | |
{ | |
var paymentsSubset = payments.Skip(0).Take(3).ToList(); | |
} | |
static void OutputRangeSimplified(List<Payment> payments) | |
{ | |
var paymentsSubset = payments[0..3]; } | |
} | |
class Payment | |
{ | |
public int Id { get; set; } | |
public decimal Amount { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment