In software development, one of the common challenges is returning multiple values from a method. Traditionally, we’ve handled this by using techniques like out parameters, creating custom classes or structs, or working with arrays and lists. However, with the introduction of tuples in C#, particularly since C# 7.0, there's a more elegant and straightforward way … Continue reading Basic C# Immutability with Tuples
Tag: c#
Why Should I Care about Mutable vs. Immutable Types
In C#, classes and objects can be categorized as mutable or immutable based on whether their state can be changed after they are created. Understanding the distinction between mutable and immutable types is crucial for designing robust, maintainable, and performant applications. Mutable Classes Mutable classes are those whose state or data can be modified after … Continue reading Why Should I Care about Mutable vs. Immutable Types
Why Should I Care About Functional Programming in C#?
Functional programming (FP) is a programming paradigm that emphasizes immutability, first-class functions, and expressions over statements. While C# is primarily an object-oriented language, it has incorporated many functional programming features over time, particularly since C# 3.0 and with further enhancements in later versions. Here's why you should care about functional programming when working with C#: … Continue reading Why Should I Care About Functional Programming in C#?
Using glob patterns in *.csproj files
Here's a cool article on simplifying your csproj files by using glob pattern for paths and file extensions https://mykeels.medium.com/using-glob-patterns-in-csproj-files-95122c7f0870 Glob patterns are like wildcard-based rules that match file paths. They let you specify file patterns using wildcards like * and ** to match multiple files with similar characteristics. In this case, we want to target all C# files (*.cs) … Continue reading Using glob patterns in *.csproj files
Using GenericHost in a C# Console Application
Introduction Building useful console applications often requires structured logging, configuration management, and dependency injection. The .NET Generic Host framework provides a powerful foundation to enable these features. Let's explore how to use GenericHost in a C# console application, with a specific focus on creating a simple command-line parser. This parser will interpret and reprint command … Continue reading Using GenericHost in a C# Console Application
Combining Array Items into String
It's always useful to be able to combine array items into a comma delimited string whether it's for output or just debugging the contents of an array or list. C# introduced a helper method to do just that, but I always seem to forget the method and how it works. So, I'm writing this down … Continue reading Combining Array Items into String
Creating a Fluent API in C#
Introduction Fluent API is a design pattern that provides more readable and intuitive code by chaining method calls together. This type of API is commonly used in implementing the Builder design pattern. This style enhances code readability, reduces boilerplate, and makes APIs easier to use. Fluent APIs are commonly seen in libraries like LINQ and … Continue reading Creating a Fluent API in C#
Parameter Validation Using ArgumentExceptions
In the latest .NET versions, argument validation has been streamlined with new helper extension methods that make it even easier and more expressive to throw argument exceptions. Some of these methods were first introduced in .NET 6 but have been enhanced with subsequent releases. Here are some of the latest ways to handle argument validation … Continue reading Parameter Validation Using ArgumentExceptions
Lesson 3.5: Creating Monsters
Our game now has a player with inventory, game items, and the ability to move between locations. But, no game is complete without some antagonists for our hero to fight. We are going to introduce monsters into the game world for the player to battle. Creating the Monster Class Let's start with a new Monster … Continue reading Lesson 3.5: Creating Monsters
Lesson 3.3: Build Inventory System
Starting with a simple inventory item list was a good way to being thinking about this problem and building the user experience to show a player's inventory. However, exposing the list directly and allowing all callers to edit the list can lead to a lot of duplicate code editing that list, and builds more interdependencies … Continue reading Lesson 3.3: Build Inventory System