CKTIS / C#
init: practical reference
init: practical reference: init allows a property to be assigned only during object initialization or construction.
Version: C# language; verified against C# 14 reference
Syntax
init /* grammar-specific form */init allows a property to be assigned only during object initialization or construction.
Use it for models intended to remain unchanged after creation.
Immutability is shallow; an assigned collection can still mutate.
required and init solve different problems, and reflection can bypass ordinary syntax.
The complete program actually uses init; its exact expected stdout is 5.
Examples
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
var item = new Item { Value = 5 }; Console.WriteLine(item.Value);
sealed class Item { public int Value { get; init; } }
Result
5
The complete program actually uses `init`; its exact expected stdout is `5`.