Blog
Mert Özen's blog posts on software development, backend, frontend, and DevOps.
Organizing C# Code: Namespaces, global using, Solutions and NuGet
Why do you get CS0117 when you reach an internal member from another project? The project and assembly boundary, file-scoped namespaces, global using and the generated implicit usings file, the .slnx solution of the .NET 10 SDK, references between projects, and adding a NuGet package.
Files and the Console in C#: ReadLine, File, Path, Directory, using and Encoding
Why does the same text take 15 and 18 bytes? UTF-8 and the BOM, Console.ReadLine that can return null, Path.Combine versus Path.Join, one-line reads and writes with File and Directory, relative paths and the working directory, and the empty file of a StreamWriter that was never disposed.
Dates and Times in C#: DateTime, DateTimeOffset, DateOnly, TimeOnly and UTC
Why do a UTC and a local DateTime with the same numbers compare equal? The Kind problem, DateTimeOffset for pinning down a moment, TimeZoneInfo and daylight saving time, DateOnly and TimeOnly from .NET 6, Days versus TotalDays in TimeSpan, culture-aware formatting and parsing, and the store-in-UTC rule.
Enums in C#: Underlying Types, [Flags], Parse and switch
The number under an enum: why (OrderStatus)7 is a valid value, why default is 0, giving values by hand, the surprises of Parse and TryParse, the CS8524 warning in switch expressions, and combinable options with [Flags].
Exception Handling in C#: try/catch/finally, throw and Your Own Exception
Exception handling in C#: try, catch and their order, cleanup with finally and using, how throw and throw ex differ in the stack trace, the when filter, writing your own exception type and using InnerException, and when to use an exception and when TryXxx.
Nullable in C#: int?, string? and Managing Null by Design
Working with null in C#: Nullable<T> and int?, lifted operators that compute with null, the nullable reference types of C# 8 and their warnings, flow analysis, the ?. ?? ??= and ! operators, and rules for managing null by design.
Value Types and Reference Types in C#: Assignment, Copying and the Special Case of string
The difference between value types and reference types in C#: what gets copied on assignment and when passing to a method, why string behaves like a value type, what is right and what is missing in the stack and heap story, equality, boxing, and struct pitfalls in collections.
Methods in C#: Parameters, Overloading, ref/out/in and params
The basic tools for writing methods in C#: parameters passed by value and return values, overloading and signatures, optional and named arguments, ref, out and in, params, local functions and why they cannot be overloaded, and expression-bodied members.
Arrays in C#: Single and Multidimensional, Jagged, Index and Range
Arrays in C#: declaring them and their default values, why an array is a reference type and how to copy one properly, multidimensional and jagged arrays, the methods of the Array class, Index and Range from C# 8, and why a Range creates a copy.
Loops in C#: for, while, do-while and foreach
The four loops in C# and when to use each: for, while, do-while and foreach. break and continue, a clean way out of nested loops, the infinite loop pattern, and why changing a collection while walking it throws an exception.
Conditions in C#: if/else, switch and Switch Expressions
The ways to make decisions in C#: why order matters in an if/else chain, braces and scope, a switch statement that forbids fall-through, the value-returning switch expression, and the relational and logical patterns of C# 9.
Operators and Expressions in C#: Short-Circuiting, ??, ?. and Precedence
Expressions versus statements in C#, arithmetic and compound assignment, prefix and postfix increments, comparison, logical operators and short-circuiting, the ?: operator, handling null with ??, ??= and ?., and precedence traps such as 100 - discount ?? 0.