Csharp

From G1Joshi/Agent-Skills · View interactive version

Csharp is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert C# and .NET assistance covering LINQ, async/await patterns, pattern matching, records, Entity Framework Core, and ASP.NET Core web services. Use when writing idiomatic C#, designing .NET enterprise backends, building cross-platform services, or optimizing memory with span/memory types.

How to use this skill

Skill file

C#

Modern C# development with .NET 8+, async patterns, and Entity Framework.

When to Use

Quick Start

public record User(string Id, string Name, string Email);

public class UserService
{
    public async Task<User?> GetUserAsync(string id)
    {
        return await _repository.FindByIdAsync(id);
    }
}

Core Concepts

Records & Pattern Matching (C# 12/13)

Immutable data structures with value equality and expressive switch expressions:

// Positional Record with built-in value equality
public record Order(string Id, decimal Amount, string Status);

public static class OrderClassifier
{
    public static string Evaluate(Order order) => order switch
    {
        { Status: "PAID", Amount: > 1000 } => "High-Value Order",
        { Status: "PAID" }                 => "Standard Order",
        { Status: "CANCELLED" }            => "Void Order",
        _                                  => "Pending Review"
    };
}

Memory Optimization with `Span<T>` and `ReadOnlySpan<T>`

Allocates and slices continuous memory buffers on the stack without heap GC allocations:

public static bool TryParseYear(ReadOnlySpan<char> dateSpan, out int year)
{
    // Slice without allocating new sub-strings
    ReadOnlySpan<char> yearSpan = dateSpan.Slice(0, 4);
    return int.TryParse(yearSpan, out year);
}

Async / Await with `ValueTask`

Efficient asynchronous programming minimizing task object allocations on hot paths:

public async ValueTask<UserProfile> GetUserProfileAsync(string userId)
{
    if (_cache.TryGetValue(userId, out var cached))
        return cached; // Synchronous return allocates zero task objects

    return await _repository.FetchFromDatabaseAsync(userId);
}

Common Patterns

Declarative Data Transformations with LINQ

Problem: Imperative nested loops and mutation when filtering, projecting, and sorting collections.

Solution:

// Query syntax
var adults = from user in users
             where user.Age >= 18
             orderby user.Name
             select user;

// Method syntax (preferred)
var result = users
    .Where(u => u.IsActive)
    .OrderBy(u => u.Name)
    .Select(u => new UserDto(u.Id, u.Name))
    .ToList();

// Grouping
var byCountry = users
    .GroupBy(u => u.Country)
    .ToDictionary(g => g.Key, g => g.ToList());

Pattern Matching

Problem: Verbose cascading conditional checks and manual type casting leading to runtime type errors.

Solution:

string GetStatus(object obj) => obj switch
{
    User { IsActive: true } => "Active user",
    User { IsActive: false } => "Inactive user",
    null => "No data",
    _ => "Unknown"
};

// List patterns
if (numbers is [var first, _, var last])
{
    Console.WriteLine($"First: {first}, Last: {last}");
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| ------------------------- | ----------------------- | --------------------------- |

| NullReferenceException | Accessing null | Enable nullable, use ?. |

| ObjectDisposedException | Using disposed resource | Check lifetime, use using |

| TaskCanceledException | Operation cancelled | Handle or propagate |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.