C# coding standard for Cadtastic Solutions.
View the Project on GitHub Cadtastic-Solutions/C-Sharp-Coding-Standard
Consistent code style improves readability, reduces cognitive load, and helps prevent common coding mistakes. These standards ensure:
// 1. File header with copyright and license
// 2. Namespace imports (sorted and grouped)
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using YourCompany.Core.Interfaces;
using YourCompany.Core.Models;
// 3. Namespace declaration
namespace YourCompany.Feature
{
// 4. Type declaration
public class FeatureManager : IFeatureManager
{
// 5. Fields (private)
private readonly ILogger<FeatureManager> _logger;
// 6. Constructors
public FeatureManager(ILogger<FeatureManager> logger)
{
_logger = logger;
}
// 7. Properties
public bool IsEnabled { get; private set; }
// 8. Public methods
public void Enable()
{
IsEnabled = true;
}
// 9. Private methods
private void LogStatus()
{
_logger.LogInformation("Feature status: {Status}", IsEnabled);
}
}
}
// Good
public class Example
{
private readonly string _name;
private readonly int _count;
public string Name => _name;
public int Count => _count;
public void ProcessItem()
{
if (_count > 0)
{
// Processing logic
}
foreach (var item in items)
{
// Loop logic
}
}
}
// Bad
public class Example {
private readonly string _name; private readonly int _count;
public string Name=>_name;
public int Count=>_count;
public void ProcessItem() {
if(_count>0){
// Processing logic
}
foreach(var item in items){
// Loop logic
}
}
}
// Good
var result = dataContext
.Users
.Where(u => u.IsActive)
.Select(u => new UserDto
{
Id = u.Id,
Name = u.Name
})
.ToList();
// Bad
var result = dataContext.Users.Where(u => u.IsActive).Select(u => new UserDto { Id = u.Id, Name = u.Name }).ToList();
// Good
if (condition)
{
DoSomething();
}
// Bad
if (condition) DoSomething();
if (condition)
DoSomething();
var when type is obvious from the right side// Good
var users = new List<User>();
var dictionary = new Dictionary<string, int>();
int count = GetCount();
// Bad
var count = GetCount();
List<User> users = new List<User>();
// Good
public string FullName => $"{FirstName} {LastName}";
public override string ToString() => FullName;
// Bad
public string FullName =>
FirstName.Length > 0
? $"{FirstName} {LastName}"
: LastName;
// Good
public void ProcessUser(User user)
{
ArgumentNullException.ThrowIfNull(user);
var name = user.Profile?.Name ?? "Unknown";
}
// Bad
public void ProcessUser(User user)
{
var name = user != null ? user.Profile != null ? user.Profile.Name : "Unknown" : "Unknown";
}
// Good
var result = value switch
{
string s => ProcessString(s),
int n => ProcessNumber(n),
null => ProcessNull(),
_ => ProcessDefault()
};
// Bad
string result;
if (value is string)
result = ProcessString((string)value);
else if (value is int)
result = ProcessNumber((int)value);
// Good
public record User(string Name, string Email);
public record class AdminUser(string Name, string Email, string[] Permissions) : User(Name, Email);
// Bad
public class User
{
public string Name { get; init; }
public string Email { get; init; }
}
// Good
// Retry operation with exponential backoff
private async Task<T> RetryOperationAsync<T>(Func<Task<T>> operation)
// Bad
// Get user
public User GetUser(int id)
# Code style rules
csharp_style_var_for_built_in_types = false:error
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = false:suggestion
# Formatting rules
csharp_new_line_before_open_brace = all
csharp_space_after_keywords_in_control_flow_statements = true