csharp-rules
free-genai-bootcamp-2025
Install / Use
npx skills add karnawis/free-genai-bootcamp-2025Installs into whichever agent you are using.
Cursor Rules
Cursor IDE rules (v2)
Quality Score
Category
Development & EngineeringSupported Platforms
Tags
Skill content
View source on GitHubC# Best Practices and Rules
Core Rules for Junior Developers
1. Always Use Proper Disposal for IDisposable Objects
// ❌ BAD: Resource not properly disposed
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
// ... do work ...
// Connection might never be closed!
// ✅ GOOD: Using statement ensures proper disposal
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// ... do work ...
} // Connection automatically disposed here
// ✅ EVEN BETTER: Modern using declaration (C# 8.0+)
using var connection = new SqlConnection(connectionString);
connection.Open();
// ... do work ...
// Connection disposed at end of scope
2. Use Null Checking and Null-Conditional Operators
// ❌ BAD: Potential NullReferenceException
public string GetUserEmail(User user)
{
return user.Email.ToLower(); // Will crash if user or Email is null
}
// ✅ GOOD: Proper null checking
public string GetUserEmail(User user)
{
if (user == null)
throw new ArgumentNullException(nameof(user));
return user.Email?.ToLower() ?? "no email";
}
// ✅ BETTER: Using C# 8.0+ null-safety
public string GetUserEmail(User user)
{
ArgumentNullException.ThrowIfNull(user);
return user.Email?.ToLower() ?? "no email";
}
3. Use Strong Typing and Avoid Magic Strings/Numbers
// ❌ BAD: Magic strings and numbers
public void ProcessOrder(string status)
{
if (status == "pending") // Magic string
{
// Process with magic number
CalculateDiscount(100); // What does 100 mean?
}
}
// ✅ GOOD: Using enums and constants
public enum OrderStatus
{
Pending,
Processing,
Completed
}
public class OrderProcessor
{
private const decimal StandardDiscountRate = 100m;
public void ProcessOrder(OrderStatus status)
{
if (status == OrderStatus.Pending)
{
CalculateDiscount(StandardDiscountRate);
}
}
}
Additional Tips
-
Exception Handling
- Use specific exception types instead of catching Exception
- Only catch exceptions you can handle
- Always include meaningful exception messages
- Use try-catch blocks only when necessary
-
Code Organization
- Follow consistent naming conventions
- Keep methods small and focused
- Use meaningful variable names
- Group related functionality into classes
-
Performance
- Use StringBuilder for string concatenation in loops
- Implement IDisposable for classes with unmanaged resources
- Use async/await for I/O operations
- Consider using value types for small, simple types
-
Common Pitfalls to Avoid
- Don't ignore exceptions
- Don't use public fields (use properties instead)
- Don't return null collections (return empty ones)
- Don't use string concatenation in loops
Related Skills
career-ops
72.4kOpen-source AI job search: scan job portals, evaluate listings into a structured A-H report with a global 1-5 score, tailor your CV, track applications — runs locally in your AI coding CLI (Claude Code, Codex, OpenCode, Antigravity…)
ai-job-search
43.6kThe job search that runs on your machine. AI job application framework built on Claude Code: evaluate postings, tailor CVs, write cover letters, prep interviews. Fork it and own it.
claude-howto
41.6kA visual, example-driven guide to Claude Code — from basic concepts to advanced agents, with copy-paste templates that bring immediate value.
guizang-ppt-skill
26.7kAI-agent Skill for generating polished HTML slide decks: editorial magazine and Swiss layouts, image prompts, social covers, and a WebGL/low-power presentation runtime.
Security Score
Audited on Invalid Date
