Why is ForEach Iteration over List<T> faster than IList<T>

In this post, I would like to draw your attention to the performance implications when iterating over IList<T> and List<T> . First let us begin by writing some code to run our benchmark tests against. We will execute a simple foreach loop against IList<T> and List<T>. [Benchmark] [ArgumentsSource(nameof(IListCollection))] public void ForEachIList(IList<int> collection) { foreach (var item in collection) { DoSomething(item); } } … Continue reading Why is ForEach Iteration over List<T> faster than IList<T>

Advertisement

Exploring ‘With’ C# 10

C# 9 introduced us to record and the associated with operator. The with operator helped us to create or clone copies of existing record instances with set of changes specified. In C# 10, the C# team has taken the concepts a little further. Let us first examine what we could do in C# 9. public record Person { public string FirstName … Continue reading Exploring ‘With’ C# 10

.Net 6 : PeriodTimer

.NET supports various timer classes, each of which offers different functionality. As Microsoft states in its documentation, System.Timers.Timer, which fires an event and executes the code in one or more event sinks at regular intervals. The class is intended for use as a server-based or service component in a multithreaded environment; it has no user … Continue reading .Net 6 : PeriodTimer

.Net 6 : ArgumentNullException.ThrowIfNull

.Net 6 has some nice little hidden features which helps in creating cleaner code, and in this post we will look into one such feature. Previously, if we needed to check if an object is null and raise an exception, we would have done as the following. void Foo(Bar bar) { if (bar is null) … Continue reading .Net 6 : ArgumentNullException.ThrowIfNull

Json To Xml : Wrapping the Array Elements

In this post, we will look into a scenario when you need to convert a Json to XML, but at the same time need to add a wrapping Property for each array. For example, consider the following Json { reportId : 23232, submittedBy : 'Anu Viswan', projects:[ { id:1001, name:'Project 1', }, { id:1002, name:'Project … Continue reading Json To Xml : Wrapping the Array Elements

Debugging C# Source Generator

In this post, we will look into how to Debug Source Generator using Visual Studio 2019. The prerequisite is to ensure you have Visual Studio 2019 16.10 or above. Prerequisite Ensure you have Visual Studio 2019 16.10 or above.Ensure .Net Compiler Platform SDK is installed. To install .Net Compiler Platform SDK, launch the Visual Studio … Continue reading Debugging C# Source Generator

Source Generator : AutoToString

In this post, we will look into an application of Source Generator. Quite often, during debugging, you would wish if you had overridden the ToString() method of the class, so that you could understand the state of the instance. These are especially useful when you are dealing with a collection of the type. The ToString() would provide you a … Continue reading Source Generator : AutoToString

.Net 6: Linq enhancements

Linq has got some noticeably enhancements in the .Net 6. In this post, we will briefly visit some of them. Specify Default Value for FirstOrDefault, LastOrDefault, SingleOrDefault One of the features I am so relieved to see if the support for specifying default value for FirstOrDefault(), LastOrDefault(), and SingleOrDefault(). I never quite understood why it wasn't included in … Continue reading .Net 6: Linq enhancements

.Net 6 : Benchmark performance of JsonSerializer.DeserializeAsyncEnumerable

This should have been part of my earlier post on System.Text.Json Support for IAsyncEnumerable, but it slipped off my mind. So here we are. To understand the significance of this feature in .Net 6, one need to understand the circumstances under which these might be useful. The first of those would be of course, that the … Continue reading .Net 6 : Benchmark performance of JsonSerializer.DeserializeAsyncEnumerable