Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created December 17, 2025 16:25
Show Gist options
  • Select an option

  • Save karenpayneoregon/bc6ac50fc4112160f8f895ce064aea2e to your computer and use it in GitHub Desktop.

Select an option

Save karenpayneoregon/bc6ac50fc4112160f8f895ce064aea2e to your computer and use it in GitHub Desktop.
Working with decimal arrays
  1. Create a new Console project and name it WorkingWithArrays
  2. Create a new folder named Classes in the project by right clicking on the project name, select Add then New Folder
  3. Right click the Classes folder, select Add then New item, name it MockedData.cs
  4. Open MockedData.cs and copy and paste the code below.
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 };
}
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