100 C# Tips I Wish I Knew Earlier (Part 1)
Source: Dev.to

Modern C# Features That Improve Your Code
1. Embrace Primary Constructors
public class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
}Cleaner, shorter, and easier to maintain.
2. Use Collection Expressions
List numbers = [1, 2, 3, 4, 5];Less typing, more clarity.
3. Use nameof for Safer Refactoring
Console.WriteLine(nameof(Person.Name));The compiler now helps you catch mistakes.
4. Use Null‑Coalescing for Safer Code
string name = person.Name ?? "Unknown";Simple and safe.
5. Pattern Matching = Cleaner Logic
if (person is { Age: > 18, Name: not null })
{
Console.WriteLine($"{person.Name} is an adult.");
}6. Prefer String Interpolation
string message = $"Hello, {person.Name}!";7. Use Tuples Instead of Extra Classes
public (string, int) GetPersonInfo()
{
return ("Alice", 30);
}Lightweight and efficient.
8. Use Local Functions for Better Structure
void Process()
{
int Add(int a, int b) => a + b;
Console.WriteLine(Add(1, 2));
}9. Use using var Instead of Nested Blocks
using var reader = new StreamReader("file.txt");Cleaner resource management.
10. Use IAsyncEnumerable for Streaming Data
public async IAsyncEnumerable GetData()
{
for (int i = 0; i
}Switch Statements
string result = age switch
{
> 18 => "Adult",
_ => "Minor"
};14. Use with for Object Updates
var updated = person with { Age = 31 };15. File‑Scoped Namespaces Reduce Noise
namespace MyApp;Cleaner files instantly.
16. Use required for Safer Models
public class Person
{
public required string Name { get; set; }
}17. Init‑Only Properties for Immutability
public string Name { get; init; }18. Global Using Saves Repetition
global using System;19. CallerArgumentExpression for Debugging
public void Validate(bool condition,
[CallerArgumentExpression("condition")] string msg = "")
{
if (!condition) throw new Exception(msg);
}20. Default Interface Methods
public interface ILogger
{
void Log(string msg) => Console.WriteLine(msg);
}Performance & Advanced Tips
21. Use Span for High Performance
Span numbers = stackalloc int[10];22. Use ArrayPool to Reduce GC Pressure
var pool = ArrayPool.Shared;
var arr = pool.Rent(10);
pool.Return(arr);23. Use ValueTask for Lightweight Async
public ValueTask GetValueAsync() => new(42);24. ConfigureAwait for Better Performance
await Task.Delay(100).ConfigureAwait(false);25. Use Stopwatch to Measure Performance
var sw = Stopwatch.StartNew();
// code to measure
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);Final Thoughts
These small improvements compound over time.
They made my code:
- Cleaner
- Faster
- Easier to maintain