diff --git a/docs/csharp/programming-guide/indexers/index.md b/docs/csharp/programming-guide/indexers/index.md index c663e77b2c0ee..bfa1d8af9a343 100644 --- a/docs/csharp/programming-guide/indexers/index.md +++ b/docs/csharp/programming-guide/indexers/index.md @@ -55,6 +55,14 @@ Another common scenario is when you need to model a dictionary or a map. This sc In this example, the `ArgsAction` collection maps closely to the underlying collection. The `get` determines if a given option is configured. If so, it returns the associated with that option. If not, it returns an that does nothing. The public accessor doesn't include a `set` accessor. Rather, the design is using a public method for setting options. +### Date-based indexers + +When working with date-based data, you can use either or as indexer keys. Use when you only need the date part and want to avoid time-related complications. The following example shows a temperature tracking system that uses `DateOnly` as the primary indexer key: + +:::code language="csharp" source="./snippets/indexers/DateOnlyIndexer.cs"::: + +This example demonstrates both `DateOnly` and `DateTime` indexers. While the `DateOnly` indexer is the primary interface, the `DateTime` overload provides convenience by extracting only the date portion. This approach ensures that all temperature data for a given day is treated consistently, regardless of the time component. + ### Multi-Dimensional Maps You can create indexers that use multiple arguments. In addition, those arguments aren't constrained to be the same type. diff --git a/docs/csharp/programming-guide/indexers/snippets/indexers/DateOnlyIndexer.cs b/docs/csharp/programming-guide/indexers/snippets/indexers/DateOnlyIndexer.cs new file mode 100644 index 0000000000000..9e9bca8ec27c4 --- /dev/null +++ b/docs/csharp/programming-guide/indexers/snippets/indexers/DateOnlyIndexer.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; + +namespace Indexers; + +public class DailyTemperatureData +{ + private readonly Dictionary _temperatureData = new(); + + // Indexer using DateOnly for date-only scenarios + public (double High, double Low) this[DateOnly date] + { + get + { + if (_temperatureData.TryGetValue(date, out var temp)) + { + return temp; + } + throw new KeyNotFoundException($"No temperature data available for {date:yyyy-MM-dd}"); + } + set + { + _temperatureData[date] = value; + } + } + + // Overload using DateTime for convenience, but only uses the date part + public (double High, double Low) this[DateTime dateTime] + { + get => this[DateOnly.FromDateTime(dateTime)]; + set => this[DateOnly.FromDateTime(dateTime)] = value; + } + + public bool HasDataFor(DateOnly date) => _temperatureData.ContainsKey(date); + + public IEnumerable AvailableDates => _temperatureData.Keys; +} \ No newline at end of file diff --git a/docs/csharp/programming-guide/indexers/snippets/indexers/DateOnlyProgram.cs b/docs/csharp/programming-guide/indexers/snippets/indexers/DateOnlyProgram.cs new file mode 100644 index 0000000000000..619898df3159b --- /dev/null +++ b/docs/csharp/programming-guide/indexers/snippets/indexers/DateOnlyProgram.cs @@ -0,0 +1,32 @@ +using System; +using Indexers; + +namespace DateOnlyExample; + +class Program +{ + static void Main() + { + var temperatureData = new DailyTemperatureData(); + + // Store temperature data using DateOnly + var today = DateOnly.FromDateTime(DateTime.Today); + temperatureData[today] = (75.2, 62.1); + temperatureData[today.AddDays(1)] = (78.5, 64.3); + temperatureData[today.AddDays(-1)] = (72.8, 59.7); + + // Retrieve data using DateOnly + Console.WriteLine($"Today's temperature: High {temperatureData[today].High}°F, Low {temperatureData[today].Low}°F"); + + // Can also use DateTime - only the date part is used + var tomorrow = DateTime.Today.AddDays(1); + Console.WriteLine($"Tomorrow's temperature: High {temperatureData[tomorrow].High}°F, Low {temperatureData[tomorrow].Low}°F"); + + // Show available dates + Console.WriteLine("Available temperature data for:"); + foreach (var date in temperatureData.AvailableDates) + { + Console.WriteLine($" {date:yyyy-MM-dd}"); + } + } +} \ No newline at end of file diff --git a/docs/csharp/programming-guide/indexers/snippets/indexers/Indexers.csproj b/docs/csharp/programming-guide/indexers/snippets/indexers/Indexers.csproj index 16e0a5ccc0d59..93d988e5f1a8c 100644 --- a/docs/csharp/programming-guide/indexers/snippets/indexers/Indexers.csproj +++ b/docs/csharp/programming-guide/indexers/snippets/indexers/Indexers.csproj @@ -5,7 +5,6 @@ net9.0 enable enable - preview