csharp-dotnet
C# and .NET development patterns
What this skill does
# C# & .NET
## Overview
Modern C# and .NET development patterns including async programming, LINQ, and ASP.NET Core.
---
## Modern C# Features
### Records and Init-Only Properties
```csharp
// Record type (immutable by default)
public record User(
string Id,
string Email,
string Name,
DateTime CreatedAt
);
// With-expressions for immutable updates
var user = new User("1", "[email protected]", "Test User", DateTime.UtcNow);
var updated = user with { Name = "Updated Name" };
// Record with validation
public record ValidatedUser
{
public required string Id { get; init; }
public required string Email { get; init; }
public required string Name { get; init; }
public ValidatedUser()
{
// Validation in constructor
}
// Custom validation
public static ValidatedUser Create(string email, string name)
{
if (!email.Contains("@"))
throw new ArgumentException("Invalid email");
return new ValidatedUser
{
Id = Guid.NewGuid().ToString(),
Email = email,
Name = name
};
}
}
// Init-only properties
public class Config
{
public required string ConnectionString { get; init; }
public int Timeout { get; init; } = 30;
}
```
### Pattern Matching
```csharp
// Type patterns
public string Describe(object obj) => obj switch
{
string s => $"String of length {s.Length}",
int i when i > 0 => $"Positive integer: {i}",
int i => $"Non-positive integer: {i}",
IEnumerable<int> list => $"Integer list with {list.Count()} items",
null => "Null value",
_ => "Unknown type"
};
// Property patterns
public decimal CalculateDiscount(Order order) => order switch
{
{ Total: > 1000, Customer.IsPremium: true } => order.Total * 0.2m,
{ Total: > 1000 } => order.Total * 0.1m,
{ Customer.IsPremium: true } => order.Total * 0.05m,
_ => 0
};
// List patterns (C# 11)
public string DescribeList(int[] numbers) => numbers switch
{
[] => "Empty",
[var single] => $"Single element: {single}",
[var first, var second] => $"Two elements: {first}, {second}",
[var first, .. var middle, var last] => $"First: {first}, Last: {last}, Middle count: {middle.Length}",
};
// Positional patterns with deconstruct
public record Point(int X, int Y);
public string Quadrant(Point point) => point switch
{
(0, 0) => "Origin",
(> 0, > 0) => "First quadrant",
(< 0, > 0) => "Second quadrant",
(< 0, < 0) => "Third quadrant",
(> 0, < 0) => "Fourth quadrant",
(_, 0) or (0, _) => "On an axis"
};
```
### Nullable Reference Types
```csharp
#nullable enable
public class UserService
{
// Non-nullable (compiler ensures not null)
public User GetUser(string id)
{
return _repository.Find(id)
?? throw new NotFoundException($"User {id} not found");
}
// Nullable return
public User? FindUser(string id)
{
return _repository.Find(id);
}
// Null handling
public string GetDisplayName(User? user)
{
// Null-conditional
var name = user?.Name;
// Null-coalescing
return user?.Name ?? "Anonymous";
// Null-coalescing assignment
// user ??= CreateDefaultUser();
}
// Null-forgiving operator (use sparingly)
public void ProcessUser(User? user)
{
// When you know it's not null but compiler doesn't
var name = user!.Name;
}
}
// Required members (C# 11)
public class RequiredUser
{
public required string Id { get; init; }
public required string Email { get; init; }
public string? Nickname { get; init; }
}
```
---
## Async Programming
```csharp
using System.Threading.Tasks;
using System.Threading;
public class AsyncService
{
// Basic async method
public async Task<User> GetUserAsync(string id, CancellationToken ct = default)
{
var response = await _httpClient.GetAsync($"/users/{id}", ct);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<User>(ct)
?? throw new InvalidOperationException("Null response");
}
// Parallel execution
public async Task<IReadOnlyList<User>> GetUsersAsync(IEnumerable<string> ids)
{
var tasks = ids.Select(id => GetUserAsync(id));
return await Task.WhenAll(tasks);
}
// With error handling
public async Task<Result<User>> SafeGetUserAsync(string id)
{
try
{
var user = await GetUserAsync(id);
return Result<User>.Success(user);
}
catch (Exception ex)
{
return Result<User>.Failure(ex.Message);
}
}
// ValueTask for potentially synchronous operations
public ValueTask<User?> GetCachedUserAsync(string id)
{
if (_cache.TryGetValue(id, out var user))
{
return ValueTask.FromResult<User?>(user);
}
return new ValueTask<User?>(FetchAndCacheUserAsync(id));
}
// Async enumerable (IAsyncEnumerable)
public async IAsyncEnumerable<User> StreamUsersAsync(
[EnumeratorCancellation] CancellationToken ct = default)
{
var page = 1;
while (true)
{
var users = await FetchPageAsync(page, ct);
if (!users.Any()) yield break;
foreach (var user in users)
{
yield return user;
}
page++;
}
}
// Consuming async enumerable
public async Task ProcessAllUsersAsync()
{
await foreach (var user in StreamUsersAsync())
{
await ProcessUserAsync(user);
}
}
}
// Channels for producer-consumer
public class ChannelExample
{
private readonly Channel<Message> _channel = Channel.CreateBounded<Message>(100);
public async Task ProduceAsync(Message message)
{
await _channel.Writer.WriteAsync(message);
}
public async Task ConsumeAsync(CancellationToken ct)
{
await foreach (var message in _channel.Reader.ReadAllAsync(ct))
{
await ProcessMessageAsync(message);
}
}
}
```
---
## LINQ
```csharp
using System.Linq;
public class LinqExamples
{
// Query syntax
public IEnumerable<User> GetActiveUsers(IEnumerable<User> users)
{
return from user in users
where user.IsActive
orderby user.Name
select user;
}
// Method syntax (more common)
public IEnumerable<string> GetActiveUserEmails(IEnumerable<User> users)
{
return users
.Where(u => u.IsActive)
.OrderBy(u => u.Name)
.Select(u => u.Email);
}
// Grouping
public IDictionary<string, List<User>> GroupByDomain(IEnumerable<User> users)
{
return users
.GroupBy(u => u.Email.Split('@')[1])
.ToDictionary(g => g.Key, g => g.ToList());
}
// Join
public IEnumerable<OrderWithUser> JoinOrdersWithUsers(
IEnumerable<Order> orders,
IEnumerable<User> users)
{
return orders.Join(
users,
order => order.UserId,
user => user.Id,
(order, user) => new OrderWithUser(order, user));
}
// Aggregate
public OrderStats GetOrderStats(IEnumerable<Order> orders)
{
return new OrderStats(
Count: orders.Count(),
Total: orders.Sum(o => o.Amount),
Average: orders.Average(o => o.Amount),
Max: orders.Max(o => o.Amount)
);
}
// SelectMany (flatten)
public IEnumerable<OrderItem> GetAllItems(IEnumerable<Order> orders)
{
return orders.SelectMany(o => o.Items);
}
// Complex pipeline
public IEnumerable<UserSummary> GetTopCustomers(
IEnumerable<User> users,
IEnumerable<Order> orders)
{
return orders
.GroupBy(o => o.UserId)
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.