Skip to content

Basic Examples

Basic configuration reading and type conversion examples.

Simple Configuration

csharp
using Apq.Cfg;

var cfg = new CfgBuilder()
    .AddJsonFile("config.json")  // Uses default level 0
    .Build();

// String value
var name = cfg["App:Name"];

// Integer value
var port = cfg.GetValue<int>("App:Port");

// Boolean value
var debug = cfg.GetValue<bool>("App:Debug");

// With default value
var timeout = cfg.GetValue<int?>("App:Timeout") ?? 30;

Configuration Sections

csharp
var dbSection = cfg.GetSection("Database");

var host = dbSection["Host"];
var port = dbSection.GetValue<int>("Port");
var name = dbSection["Name"];

Console.WriteLine($"Database: {host}:{port}/{name}");

Check Existence

csharp
if (cfg.Exists("App:OptionalFeature"))
{
    var feature = cfg["App:OptionalFeature"];
    // Use feature
}

Next Steps

Released under the MIT License