Use local functions in C# to encapsulate logic to a single calling method

💡 – Did you know we can use local functions in C# to scope logic only to their containing methods? For readability it can be a big help to break up large methods into smaller well-named methods. When we use private methods for this however, they are scoped privately to the class when really they … Continue reading Use local functions in C# to encapsulate logic to a single calling method

Get method caller info in C# using info attributes

C# Tip 💡 – Did you know we can get info about the caller of a method using C# (5+) info attributes? The example below shows the caller member name, file path and line number being outputted as we have annotated the parameters of the TraceMessage method with the CallerMemberName, CallerFilePath and CallerLineNumber attributes. These … Continue reading Get method caller info in C# using info attributes

C# Explicit Operator example to map from one type to another

In .NET we have plenty of ways to map objects to one another. Below is an example, courtesy of ChatGPT of using the EXPLICIT OPERATOR 👇🏻. How do you like to convert objects from one type to another? AutoMapper? Mapster? Plain left-right assignment perhaps using the Explicit Operator or Extension methods? The words are often … Continue reading C# Explicit Operator example to map from one type to another

C# – How to check if an item exists in a list

In this article we’ll present and compare two functions which offer us a possibility to check whether an item exists in a given list. These functions are Exists and Contains, both available in System.Collections.Generic namespace. Provided examples are for two different lists: one containing integers and other one containing objects of custom created class. List … Continue reading C# – How to check if an item exists in a list