Skip to content
Back to skills

Language

ASecurity

Modern C# standards for type safety, performance, and maintainability.

  • 8 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 8, 2026
ai-agentsc#sqlexpresssecurityperformance

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned September 8, 2026

npx -y skills add ngxtm/devkit --skill language --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Language?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Language
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ngxtm-language-devkit/badge)](https://www.skillsdirectory.com/skills/ngxtm-language-devkit)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: C# Language Patterns
description: Modern C# standards for type safety, performance, and maintainability.
metadata:
  labels: [csharp, dotnet, language, types, async]
  triggers:
    files: ['**/*.cs', '**/*.csproj']
    keywords: [class, record, interface, async, await, linq, nullable, span]
---

# C# Language Patterns

## **Priority: P0 (CRITICAL)**

Modern C# standards for type-safe, performant, maintainable code.

## Implementation Guidelines

- **Nullable Reference Types**: Enable `<Nullable>enable</Nullable>`. Use `?` for nullable, avoid `!` except when compiler can't infer.
- **Records**: `record` for immutable DTOs, `record struct` for stack-allocated value types.
- **Pattern Matching**: `is` patterns, `switch` expressions, property/positional patterns.
- **Async/Await**: Always use `CancellationToken`. `ValueTask` for hot paths. `ConfigureAwait(false)` in libraries.
- **LINQ**: Prefer method syntax. Avoid multiple enumerations (`ToList()` once). Use `AsNoTracking()` for read-only EF queries.
- **Generics**: Constraints (`where T : class, new()`), covariance (`out T`), contravariance (`in T`).
- **Spans**: `Span<T>`, `ReadOnlySpan<T>` for zero-allocation slicing.
- **Primary Constructors**: C# 12+ `class Foo(int x)` for concise DI.
- **Collection Expressions**: C# 12+ `[1, 2, 3]` syntax.
- **Raw String Literals**: `"""multi-line"""` for SQL, JSON templates.

## Anti-Patterns

- **No `async void`**: Use `async Task`. Exception: event handlers.
- **No `Task.Result`/`.Wait()`**: Deadlock risk. Always `await`.
- **No `DateTime.Now`**: Use `DateTimeOffset.UtcNow` for timezone safety.
- **No string concat in loops**: Use `StringBuilder` or `string.Join`.
- **No `!` abuse**: Prefer null checks or `??` over null-forgiving.

## Code

```csharp
// Record with primary constructor
public record UserDto(string Name, string Email);

// Pattern matching with switch expression
string GetStatus(Order order) => order switch
{
    { Status: OrderStatus.Pending } => "Waiting",
    { Status: OrderStatus.Shipped, TrackingNumber: not null } => "In Transit",
    { IsCancelled: true } => "Cancelled",
    _ => "Unknown"
};

// Async with cancellation token
async Task<User?> GetUserAsync(int id, CancellationToken ct = default)
{
    return await _db.Users
        .AsNoTracking()
        .FirstOrDefaultAsync(u => u.Id == id, ct);
}

// Span for zero-allocation parsing
ReadOnlySpan<char> GetFirstWord(ReadOnlySpan<char> text)
{
    int idx = text.IndexOf(' ');
    return idx < 0 ? text : text[..idx];
}

// Primary constructor (C# 12)
public class UserService(IUserRepository repo, ILogger<UserService> logger)
{
    public async Task<User?> GetAsync(int id) => await repo.GetByIdAsync(id);
}
```

## Reference & Examples

For advanced patterns, spans, and nullable annotations:
See [references/REFERENCE.md](references/REFERENCE.md).

## Related Topics

best-practices | security | tooling

Files in this skill

  • SKILL.md2.9 KB
  • references/REFERENCE.md5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.