csharp-coding-style
Use when writing, reviewing, or changing C#/.NET code in this repo, especially packages/source-windows Core, Helper, ControlPanel, .csproj, .editorconfig, NativeAOT, nullable, async/threading, and modern C# feature decisions. Pair with coding-style for language-neutral naming, comments, tests, and ownership rules.
- License 许可
- GPL-3.0
- 最近更新
- 2026-10-04
- Stars
- ★ 21
- Forks
- 2
01 前言 Front Matter
- name
- csharp-coding-style
- description
- Use when writing, reviewing, or changing C#/.NET code in this repo, especially packages/source-windows Core, Helper, ControlPanel, .csproj, .editorconfig, NativeAOT, nullable, async/threading, and modern C# feature decisions. Pair with coding-style for language-neutral naming, comments, tests, and ownership rules.
02 资源文件 1
- 参考文档THIRD_PARTY_LICENSE.md1.1 KB
03 SKILL.md 正文
C# Coding Style
Use this skill for C#/.NET-specific decisions. Use coding-style for language-neutral naming, comments, tests, and ownership rules.
Baseline: follow dotnet/runtime C# style unless this repo overrides it. Use Microsoft Learn C# conventions where runtime style is silent. Prefer .editorconfig and built-in analyzers over prose-only rules.
Rules here are defaults, not blind transformations. If generated code, WinUI/XAML requirements, dependency APIs, NativeAOT limitations, or a file's clear local style conflicts with a rule, preserve correctness and state the reason. Where examples use Too rigid, that means mechanically following the rule would make the code worse.
1. Project Defaults
-
PREFER: Target .NET 10 and C# 14 for new Windows source projects unless a dependency, packaging, or Windows SDK constraint requires otherwise.
- Good:
<TargetFramework>net10.0-windows</TargetFramework> - Bad:
<TargetFramework>netstandard2.0</TargetFramework>for the Windows hardware source. - Too rigid: using
net10.0-windows10.0.19041.0inHelperjust becauseControlPanelneeds WinUI/Windows SDK APIs.
- Good:
-
DO: Enable nullable reference types. Treat non-nullable references as the default. Use
?only when null is a real domain state or boundary result.- Good:
public string? ParentSensorId { get; init; } - Bad:
public string SensorId { get; init; } = null!; - Too rigid:
public string ParentSensorId { get; init; } = "";when "no parent sensor" is a real state.
- Good:
-
PREFER: Keep
Corefree of UI, CLI, and Node IPC concerns. LHM/PawnIO access, sensor cataloging, and metric mapping belong inCore;HelperandControlPanelconsumeCore.- Good:
CorereturnsIReadOnlyList<SensorSnapshot>. - Bad:
CorereferencesMicrosoft.UI.Xamlor writes toConsole.Out. - Too rigid: moving
SensorSnapshotout ofCoreonly becauseHelperlater serializes it.
- Good:
2. Types And Construction
-
PREFER: Make internal and private classes
sealedby default. Leave a type unsealed for a real inheritance, framework, proxy, or test-substitution need.- Good:
internal sealed class LibreHardwareReader - Bad:
internal class SensorMapperwith no subclassing requirement. - Too rigid:
public sealed class HardwareSourcewhenLibreHardwareSource : HardwareSourceis the intended extension point.
- Good:
-
PREFER: Use
requiredplusinitfor object-initialized DTOs and options. Use constructors or factories when invariants span multiple values or construction has behavior.- Good:
public required string Identifier { get; init; } - Bad:
public string Identifier { get; set; } = "";on an immutable snapshot DTO. - Too rigid:
public required double Minimum { get; init; } public required double Maximum { get; init; }when a constructor must validateminimum <= maximum.
- Good:
-
PREFER: Use records only for immutable values and results. Avoid records for hardware owners, long-lived runtime state, mutable caches, and types where
withcopying hides cost or ownership.- Good:
public readonly record struct SensorKey(string HardwareId, string SensorId); - Bad:
public record HardwareMonitorSession(Computer Computer); - Too rigid: replacing every immutable result with
recordwhen it owns large mutable arrays or disposable handles.
- Good:
-
PREFER: Use primary constructors for small sealed types or records when parameters only initialize fields/properties or pass to
base. In larger types, assign constructor parameters to explicitly named_camelCasefields so ownership stays visible.- Good:
public readonly record struct SensorReading(string SensorId, double Value); - Bad:
public readonly record struct SensorReading { public SensorReading(string sensorId, double value) { SensorId = sensorId; Value = value; } public string SensorId { get; } public double Value { get; } } - Too rigid:
internal sealed class ControlPanelViewModel(IHardwareReader reader, ISettingsStore settingsStore, ILogger logger, IDispatcher dispatcher, IFilePicker filePicker) { }
- Good:
-
CONSIDER: Use the C# 14
fieldkeyword only for local property validation. Use an explicit backing field when the state needs a domain name, debugging clarity, or multi-accessor behavior.- Good:
public int PollingIntervalSeconds { get; set => field = value > 0 ? value : throw new ArgumentOutOfRangeException(nameof(value)); } - Bad:
private int _pollingIntervalSeconds; public int PollingIntervalSeconds { get => _pollingIntervalSeconds; set => _pollingIntervalSeconds = value > 0 ? value : throw new ArgumentOutOfRangeException(nameof(value)); } - Too rigid:
set => field = NormalizePollingInterval(value);when several other members also need the normalized value.
- Good:
3. NativeAOT And Trimming
-
DO: Treat NativeAOT and trimming warnings as design-affecting. Fix them at the boundary, or annotate the narrow incompatible member with
RequiresDynamicCode,RequiresUnreferencedCode, orDynamicDependencyand explain why it remains.- Good:
[JsonSerializable(typeof(SensorSnapshot))] internal partial class SourceJsonContext : JsonSerializerContext - Bad:
<NoWarn>IL2026;IL3050</NoWarn>to silence the whole project. - Too rigid: rejecting LHM integration because one adapter method needs
[RequiresUnreferencedCode("LibreHardwareMonitor reflection usage is isolated here.")].
- Good:
-
AVOID: Runtime type construction or reflection dispatch in production paths. Use it only when isolated, annotated, and tested under NativeAOT publish.
- Good:
private static readonly Dictionary<string, Func<IMetricReader>> ReaderFactories = ...; - Bad:
Activator.CreateInstance(typeNameFromSettings) - Too rigid: replacing
typeof(SensorSnapshot).Namebecause alltypeof(...)usage was mistaken for reflection dispatch.
- Good:
-
PREFER: Use explicit factories, interface dispatch, source generators, and
System.Text.Jsonsource generation instead of reflection-heavy discovery.- Good:
JsonSerializer.Serialize(snapshot, SourceJsonContext.Default.SensorSnapshot) - Bad:
JsonSerializer.Serialize(snapshot)in a NativeAOT-published helper. - Too rigid: adding a source generator for test-only JSON fixtures that are not published with NativeAOT.
- Good:
-
AVOID:
TypeDescriptor,DataContractSerializer,BinaryFormatter,Reflection.Emit, and dynamic assembly loading in Windows source projects.- Good:
JsonSerializer.Deserialize(json, SourceJsonContext.Default.SensorSnapshot) - Bad:
new DataContractSerializer(typeof(SensorSnapshot)) - Too rigid: deleting
LibreHardwareMonitor.Hardware.Computerusage solely because the package may use reflection internally; isolate the adapter instead.
- Good:
-
PREFER: Use source-generated interop for NativeAOT boundaries. Use
LibraryImportfor new P/Invoke and CsWinRT or source-generatedComWrappersfor COM/WinRT instead of built-in COM interop.- Good:
[LibraryImport("kernel32", SetLastError = true)] private static partial nint GetCurrentProcess(); - Bad:
[ComImport] interface IWmiLocator { ... }in a NativeAOT-published helper. - Too rigid: rewriting a dependency-owned interop layer that is already isolated and tested under NativeAOT.
- Good:
-
CONSIDER: Use
UnsafeAccessoronly at a narrow interop or test boundary when an explicit API, factory, or source generator cannot express the required access.- Good:
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ReadMsr")] private static extern int ReadMsr(Cpu cpu); - Bad:
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_cache")] private static extern ref Dictionary<string, object> Cache(object owner); - Too rigid: adding
public int ReadMsrForTests()to production APIs only to avoid a test-boundaryUnsafeAccessor.
- Good:
4. Async And Threading
-
DO: Accept
CancellationTokenas the last parameter on public or internal async, polling, blocking, or I/O methods when the caller can control lifetime. Do not make it optional on Core/Helper APIs that participate in shutdown.- Good:
Task<IReadOnlyList<SensorSnapshot>> ReadAsync(CancellationToken cancellationToken) - Bad:
Task PollAsync()for a long-running polling loop. - Too rigid:
double NormalizePercentage(double value, CancellationToken cancellationToken)for pure CPU math.
- Good:
-
DO: Use
ConfigureAwait(false)in Core and Helper library code. Do not use it in WinUI code that must resume on the UI thread.- Good:
await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false); - Bad:
await process.WaitForExitAsync(cancellationToken);in Core/Helper library code. - Too rigid:
await RefreshUiAsync().ConfigureAwait(false); statusText.Text = "Ready";
- Good:
-
DON'T: Use
async voidexcept for event handlers. Event handlers must catch and report exceptions at the UI or process boundary.- Good:
private async void OnRefreshClicked(object sender, RoutedEventArgs args) { try { await RefreshAsync(); } catch (Exception exception) { ReportError(exception); } } - Bad:
public async void PollAsync() - Too rigid: refusing
async voidfor a WinUI event handler and inventing awkward sync wrappers.
- Good:
-
DON'T: Block on async work with
.Result,.Wait(), or.GetAwaiter().GetResult()except at an explicit synchronous process boundary with a documented reason.- Good:
await RunAsync(cancellationToken).ConfigureAwait(false); - Bad:
ReadAsync(cancellationToken).Result - Too rigid: rewriting a required synchronous host entry point instead of using
RunAsync(cancellationToken).GetAwaiter().GetResult(); // Host requires sync entry.
- Good:
-
PREFER: Return
TaskorTask<T>by default. UseValueTask<T>only for measured hot paths or APIs that usually complete synchronously and have clear single-consumption semantics.- Good:
Task<SensorSnapshot> ReadAsync(CancellationToken cancellationToken) - Bad: storing
ValueTask<SensorSnapshot>in a field, returning it to multiple consumers, or awaiting it twice. - Too rigid: refusing
ValueTask<T>for a measured hot-path cache lookup that usually completes synchronously.
- Good:
-
PREFER: Use a dedicated
System.Threading.Lockfor synchronous private locks in .NET 9+ / C# 13+ code. Use async-compatible coordination, such asSemaphoreSlim, when the protected operation awaits.- Good:
private readonly Lock _gate = new(); - Bad:
private readonly object _lock = new();in new .NET 9+ / C# 13+ code without a compatibility reason. - Too rigid: using
lock (_gate) { await RefreshAsync(); }instead ofSemaphoreSlim.
- Good:
-
PREFER: Match disposal syntax to the resource and scope. Use
await usingwhenever the type implementsIAsyncDisposable, even if it also implementsIDisposable; the synchronous path may skip async cleanup. Useusing varwhen the enclosing block is the intended synchronous lifetime; use ausing (...)statement when early disposal matters.- Good:
await using HardwareMonitorSession session = await HardwareMonitorSession.OpenAsync(cancellationToken); - Bad:
using HardwareMonitorSession session = await HardwareMonitorSession.OpenAsync(cancellationToken);when the session implementsIAsyncDisposable. - Bad:
using var session = new HardwareMonitorSession();inside an async method whenHardwareMonitorSessionimplementsIAsyncDisposable. - Too rigid: wrapping every disposable in
using (...) { }even when method-scopeusing varis clearer and equivalent.
- Good:
-
CONSIDER: Use
IAsyncEnumerable<T>for real streams. Keep one-shot snapshots asTask<IReadOnlyList<T>>.- Good:
await foreach (SensorSnapshot snapshot in session.ReadSnapshotsAsync(cancellationToken)) { ... } - Bad: repeatedly returning growing
Task<IReadOnlyList<SensorSnapshot>>batches for an unbounded polling stream. - Too rigid: exposing
IAsyncEnumerable<T>for a single hardware snapshot request.
- Good:
5. Span And Ref Safety
-
PREFER: Use
ReadOnlySpan<T>orSpan<T>for hot in-process parsing and formatting helpers. Keep spans,ref struct, andscopedvalues out of JSON, IPC, UI, and long-lived domain contracts unless profiling or interop requires them.- Good:
static bool TryParseSensorPath(ReadOnlySpan<char> path, out SensorPath result) - Bad:
public ReadOnlySpan<char> SensorPath { get; init; }on a JSON DTO. - Too rigid: changing
public required string SensorPath { get; init; }to span-based DTO plumbing for theoretical performance.
- Good:
-
CONSIDER: Use
params ReadOnlySpan<T>only for low-level helpers where caller convenience avoids repeated array allocations.- Good:
static bool IsAny(SensorType value, params ReadOnlySpan<SensorType> allowed) - Bad:
public SensorQuery Include(params ReadOnlySpan<SensorType> sensorTypes)on a durable public domain API. - Too rigid: keeping
static bool IsAny(SensorType value, params SensorType[] allowed)in a measured hot helper.
- Good:
-
CONSIDER: Use
allows ref structonly in generic helpers that already operate on spans or otherref structvalues. Keep it out of ordinary domain APIs.- Good:
static bool TryRead<TReader>(scoped ref TReader reader) where TReader : allows ref struct - Bad:
MetricRepository<T> where T : allows ref struct - Too rigid: duplicating parser helpers for each span-backed reader only to avoid
allows ref struct.
- Good:
6. Local C# Syntax
-
PREFER: Use modern argument guards and pattern null checks. Use
ThrowIf...helpers for ordinary parameter validation, andis null/is not nullfor explicit null tests.- Good:
ArgumentNullException.ThrowIfNull(snapshot); - Good:
if (snapshot is null) return; - Bad:
if (snapshot == null) throw new ArgumentNullException(nameof(snapshot)); - Too rigid: replacing a domain-specific validation error with
ArgumentNullException.ThrowIfNull(...).
- Good:
-
DO: Follow the dotnet/runtime
varrule: usevaronly when the type is explicitly named on the right-hand side, typicallynewor an explicit cast. Do not usevarfor method return values, LINQ results, or built-in scalar literals.- Good:
var reader = new LibreHardwareReader(); - Bad:
var snapshot = reader.Read(); - Bad:
var retryLimit = 3;
- Good:
-
PREFER: Use target-typed
new()only when the left-hand side explicitly names the type.- Good:
List<SensorSnapshot> snapshots = new(); - Bad:
List<SensorSnapshot> snapshots = new List<SensorSnapshot>();
- Good:
-
PREFER: Use collection expressions for target-typed arrays, lists, and spans. Avoid them when the target type is not obvious or allocation behavior matters to the code being reviewed.
- Good:
SensorType[] supportedTypes = [SensorType.Load, SensorType.Temperature]; - Bad:
var supportedTypes = [SensorType.Load, SensorType.Temperature]; - Too rigid: replacing
ImmutableArray<SensorType>.Emptywith[]without checking the target type and allocation semantics.
- Good:
-
PREFER: Use LINQ method chains, not query syntax.
- Good:
sensors.Where(sensor => sensor.Type == SensorType.Temperature).Select(sensor => sensor.Value) - Bad:
from sensor in sensors where sensor.Type == SensorType.Temperature select sensor.Value
- Good:
-
PREFER: Use pattern matching for simple null/type/enum dispatch. Use
switchexpressions for value-producing dispatch; keepswitchstatements orifchains when side-effect order is clearer.- Good:
return sensor.Type switch { SensorType.Load => "load", SensorType.Temperature => "temp", _ => "unknown" }; - Good:
if (value is SensorSnapshot snapshot) { ... } - Bad:
SensorSnapshot? snapshot = value as SensorSnapshot; if (snapshot is not null) { ... } - Too rigid: forcing a large side-effecting workflow into a
switchexpression.
- Good:
-
DO: Use PascalCase for C# constants except interop constants that intentionally mirror an external API name.
- Good:
private const int DefaultPollingIntervalSeconds = 1; - Bad:
private const int DEFAULT_POLLING_INTERVAL_SECONDS = 1; - Too rigid: renaming
WM_DEVICECHANGEwhen intentionally mirroring Win32.
- Good:
-
DO: Use
nameof(...)for strings that refer to C# members, parameters, or properties.- Good:
throw new ArgumentOutOfRangeException(nameof(pollingIntervalSeconds)); - Bad:
throw new ArgumentOutOfRangeException("pollingIntervalSeconds");
- Good:
-
PREFER: Keep global usings limited to namespaces that are truly universal for the project. Do not add a global using to save one local
using.- Good:
using LibreHardwareMonitor.Hardware;only in the LHM adapter file. - Bad:
global using LibreHardwareMonitor.Hardware;for one adapter. - Too rigid: banning
global using System;in a project that consistently uses implicit/global base usings.
- Good:
7. Organization And Caller Readability
-
PREFER: Order class members consistently: nested types/events, static/const/readonly fields, instance fields/properties, constructors, methods. Within a group, prefer
public,internal,protected, thenprivate; keep related interface implementation members together without adding#regionunless the file already uses regions.- Good: constants and fields before constructors; public API before private helpers in the same member group.
- Bad: private helpers before fields, constructor between unrelated methods, and interface members scattered through the class.
- Too rigid: moving a private helper far away from the only method that uses it when local readability is better.
-
DO: Keep call sites self-explanatory when arguments are non-obvious. Use named arguments for boolean/null literals, named constants for repeated literals, and local variables for complex expressions.
- Good:
service.Start(useSsl: true); - Good:
TimeSpan timeout = TimeSpan.FromSeconds(5); client.Connect(endpoint, timeout); - Bad:
service.Start(true); - Bad:
client.Connect(endpoint, TimeSpan.FromMilliseconds(Math.Max(100, retryCount * 250)));
- Good:
-
PREFER: Use the narrowest truthful collection type at API boundaries. Inputs should not require
List<T>unless the method needs list-specific mutation or indexing. Outputs should communicate ownership:IReadOnlyList<T>for snapshots,List<T>when transferring a mutable container, arrays for fixed-size buffers or constants.- Good:
void Publish(IReadOnlyList<SensorSnapshot> snapshots) - Bad:
void Publish(List<SensorSnapshot> snapshots)when the method only enumerates. - Too rigid:
IEnumerable<SensorSnapshot>when the method indexes or enumerates the sequence multiple times.
- Good:
-
DO: Use formatter-stable C# layout. Use 4-space indentation, braces on control statements, one statement per line, and one blank line between members. Prefer the repo's
.editorconfigif one is added later.- Good:
if (isReady) { ... }with braces before formatting expands it. - Bad:
if (isReady) Start();
- Good:
04 同仓库更多 Skills
typescript-coding-style
Use when writing, reviewing, or changing TypeScript code in this repo, especially for TypeScript type-safety boundaries, exports, TSDoc, React Property Inspector code, and hub directory placement. Pair with coding-style for language-neutral naming, comments, tests, and ownership rules.
technical-architecture
Read this whenever you need to make any high level code changes, understand why a design choice is made.
systeminformation
Refer when you need to use the systeminformation API.
stream-deck-sdk-v2
This skill is for stream deck SDK Docs. Refer when you need to make stream deck plugin code changes.
proto
Read this before designing/writing .proto files. Do not read if you are simply consuming proto.
performance
Use when changing lifecycle hooks, polling, rendering, caching, startup, IPC, benchmarks, throttling, queueing, or any code that may run often enough to affect responsiveness or resource usage.