- 
                Notifications
    
You must be signed in to change notification settings  - Fork 6.1k
 
Add DateOnly guidance and examples to indexers documentation #47727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a0d5870
              
                Initial plan
              
              
                Copilot 7f27f20
              
                Add DateOnly example and documentation to indexers article
              
              
                Copilot 5742d7b
              
                Revert project files to use .NET 9 and modern syntax as requested
              
              
                Copilot c978ed1
              
                Remove LangVersion preview as no preview features are used
              
              
                Copilot File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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
    
  
  
    
              
        
          
  
    
      
          
            37 changes: 37 additions & 0 deletions
          
          37 
        
  docs/csharp/programming-guide/indexers/snippets/indexers/DateOnlyIndexer.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| 
     | 
||
| namespace Indexers; | ||
| 
     | 
||
| public class DailyTemperatureData | ||
| { | ||
| private readonly Dictionary<DateOnly, (double High, double Low)> _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<DateOnly> AvailableDates => _temperatureData.Keys; | ||
| } | 
        
          
  
    
      
          
            32 changes: 32 additions & 0 deletions
          
          32 
        
  docs/csharp/programming-guide/indexers/snippets/indexers/DateOnlyProgram.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -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}"); | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
              
              
        
          
          
            9 changes: 7 additions & 2 deletions
          
          9 
        
  docs/csharp/programming-guide/indexers/snippets/indexers/indexer-2.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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
    
  
  
    
              
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.