I generally don’t like too much ‘sugar’ being added to C# but Switch Expressions from C# 8 really can improve readability a lot … Are you using switch expressions in your apps? Example below is courtesy of ChatGPT 😉 …
Author: csharp.academy
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
File scoped types new in C# 11
File scoped types are new in C#11 and allow us to create types whose visibility is scoped to the source file in which it is declared. What do you think about this new C# feature?
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# – LINQ Average Examples
Average is a LINQ functionality which calculates average value of given collection. It works with both collections of primitive values and complex objects. Below article provides example of each one. List of integers var items = new List<int>() { 1, 2, 3, 4, 5 }; 1 var items = new List<int>() { 1, 2, 3, … Continue reading C# – LINQ Average Examples
C# – LINQ Sum Examples
Sum is a LINQ functionality which calculates total number of numeric items of a collection. In this article you can find examples of usage with list of primitive types (in our case integers) and also list of complex types. List of integers var items = new List<int>() { 1, 2, 3, 4, 5 }; 1 … Continue reading C# – LINQ Sum Examples
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
C# – How to convert string to enum
In this article we’ll explain the way of converting string to any enumeration type. There are two easiest ways to do so: using Parse or TryParse method. Examples of both are presented below. If you need to read how to create and use enums, please see following page: How to use enum. All examples will … Continue reading C# – How to convert string to enum
C# – How to use enum
Enum is value type containing a set of constants representing associated integers. These integers can be assigned automatically, starting from 0 and incremented by 1 on consecutive items, or assigned directly by programmer. This article presents the way of implementing our own enum and also examples of its usage. Enumeration type can be defined by … Continue reading C# – How to use enum