- Create a new
Consoleproject and name itWorkingWithArrays - Create a new folder named
Classesin the project by right clicking on the project name, selectAddthenNew Folder - Right click the
Classesfolder, selectAddthenNew item, name itMockedData.cs - Open
MockedData.csand copy and paste the code below.
Created
December 17, 2025 16:25
-
-
Save karenpayneoregon/bc6ac50fc4112160f8f895ce064aea2e to your computer and use it in GitHub Desktop.
Working with decimal arrays
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
| using System.Text.Json; | |
| namespace WorkingWithArrays.Classes; | |
| internal class MockedData | |
| { | |
| public static decimal[] GetDecimals() | |
| => | |
| [ | |
| 99.40m, 98.62m, 98.13m, 96.31m, 85.59m, 78.95m, 62.73m, 58.23m, 57.36m, 57.03m, | |
| 50.41m, 49.46m, 35.86m, 30.07m, 29.84m, 27.56m, 25.73m, 17.10m, 13.95m, 10.32m | |
| ]; | |
| public static void WriteDecimalsToJson(string filePath, decimal[] values) | |
| { | |
| var options = new JsonSerializerOptions | |
| { | |
| WriteIndented = true | |
| }; | |
| string json = JsonSerializer.Serialize(values, options); | |
| File.WriteAllText(filePath, json); | |
| } | |
| public static decimal[] ReadDecimalsFromJson(string filePath) | |
| { | |
| if (!File.Exists(filePath)) | |
| throw new FileNotFoundException("JSON file not found.", filePath); | |
| string json = File.ReadAllText(filePath); | |
| return JsonSerializer.Deserialize<decimal[]>(json) | |
| ?? throw new InvalidDataException("Failed to deserialize decimal array."); | |
| } | |
| public static JsonSerializerOptions JsonSerializerOptions | |
| => new() { WriteIndented = true }; | |
| } | |
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
| using System.Text.Json; | |
| namespace WorkingWithArrays; | |
| internal class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| if (!Directory.Exists("Json")) | |
| { | |
| Directory.CreateDirectory("Json"); | |
| } | |
| var fileName = Path.Combine("Json", "decimals.json"); | |
| var decimals = Classes.MockedData.GetDecimals(); | |
| Classes.MockedData.WriteDecimalsToJson(fileName, decimals); | |
| var readDecimals = Classes.MockedData.ReadDecimalsFromJson(fileName); | |
| var json = JsonSerializer.Serialize(readDecimals, Classes.MockedData.JsonSerializerOptions); | |
| Console.WriteLine(json); | |
| Console.WriteLine("Press ENTER to exit"); | |
| Console.ReadLine(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment