C# Tip: Initialize lists size to improve performance
Lists have an inner capacity. Every time you add more items than the current Capacity, you add performance overhead. How to prevent it?
Lists have an inner capacity. Every time you add more items than the current Capacity, you add performance overhead. How to prevent it?
By using list patterns on an array or a list you can check whether a it contains the values you expect in a specific position.
In C#, nameof can be quite useful. But it has some drawbacks, if used the wrong way.
If you need a sequence of numbers, you can pick two ways: use a While loop, or use Enumerable.Range
LINQ is a set of methods that help developers perform operations on sets of items. There are tons of methods - do you know which is the one for you?
When you need to fire synchronous events, don't use a while(true) loop: use a Timer!
C#, as every other language, has several reserved keywords. Did you know that you can use them if you use the `@` prefix?
Every application relies on some configurations. Many devs set them up using only the appsettings file. But there's more!
APIs often call other APIs to perform operations. If an error occurs in one of them, how can you understand the context that caused that error? You can use Correlation IDs in your logs!
Miniprofiler is a nice tool to profile your code in a smart way.
Propagating HTTP Headers can be useful, especially when dealing with Correlation IDs. It's time to customize our HttpClients!
LINQPad is one of the tools I use daily. But still, I haven't used it at its full power. And you?
SelectMany is one of the LINQ methods I've used the least. I couldn't get it! Turns out it was actually incredibly simple.
A suitable constructor for type 'X' could not be located. What a strange error message! Luckily it's easy to solve.
Logs are important. Properly structured logs can be the key to resolving some critical issues. With Serilog's Scopes, you can enrich your logs with info about the context where they happened.
Initializing HttpClient instances can expose you to Socket Exhaustion problems. You should use IHttpClientFactory instead
In unit tests, sometimes you need to perform deep checks on the object passed to the mocked service. We will learn 3 ways to do that with Moq and C#
How to get all the keys of an ExpandoObject? Convert it to Dictionary!
If your application is exposed on the Web, I guess that you get some values from the HTTP Requests, don't you?
It would be great if we could break the debugging flow if a condition is (not) met. Can we? Of course!
With Entity Framework you can perform operations on relational databases without writing a single line of SQL. We will use EF to integrate PostgreSQL in our application
We all need to parse strings as integers. Most of the time, we use int.TryParse(string, out int). But there's a more advanced overload that we can use for complex parsing.
What can you do if you need to temporarily change the CurrentCulture in C#?
Mapping every SQL result to a data type can be a pain. To simplify our life, we can use an ORM like Dapper to automatically map the data.
Instead of using if-else or switch blocks to handle exceptions, you can do it gracefully using the 'when' keyword.
Once we have a Postgres instance running, we can perform operations on it. We will use Npgsql to query a Postgres instance with C#
Yield is a keyword that allows you to return an item at the time instead of creating a full list and returning it as a whole.
Is your application slow? How to find bottlenecks? If so, you can use MiniProfiler to profile a .NET API application and analyze the timings of the different operations.
JSONL is JSON's less famous sibling: it allows you to store JSON objects separating them with new line. We will learn how to parse a JSONL string with C#.
Using the right data structure is crucial to building robust and efficient applications. So, why use a List or a HashSet to sort items (and remove duplicates) when you have a SortedSet?
Did you know that in .NET you can resolve specific dependencies using Factories? We'll use them to switch between concrete classes based on the current HTTP Request
Sometimes you need to ping some endpoints. Don't rely on HttpClient, but use the native Ping class.
Mocking IHttpClientFactory is hard, but luckily we can use some advanced features of Moq to write better tests.
Sometimes we need to use objects with the same name but from different namespaces. How to remove that ambiguity? By Using Aliases!
Creating simple DateTimes creates issues when handling timezones. You can solve some issues by using DateTimeKind
Debugging our .NET applications can be cumbersome. With the DebuggerDisplay attribute we can simplify it by displaying custom messages.
Is your string really empty, or has it hidden characters? With String.IsNullOrEmpty and String.IsNullOrWhiteSpace you can find it
Senders and Receivers handle errors on Azure Service Bus differently. We'll see how to catch them, what they mean and how to fix them. We'll also introduce Dead Letters.
Queues or Topics? How are they similar and how they are different? We'll see how to use those capabilities in Azure Service Bus with .NET and C#
Do you need the index of the current item in a foreach loop with C#? Here you'll see two approaches.
Azure Service bus is a message broker generally used for sharing messages between applications. In this article, we're gonna see an introduction to Azure Service Bus, and how to work with it with .NET and C#
Knowing the Assembly Version of the API you've deployed on an environment may be helpful for many reasons. We're gonna see why, how to retrieve it, and how to expose it with Endpoint Routing (bye-bye Controllers and Actions!)
Language details may impact application performance. In this article we'll see some of the C# tips that brought me to improve my application. Singleton creation, StringBuilder and more!
Having fast API response is crucial for your applications. In this article you'll see how I managed to improve an API application that took 14 secs each call.
DateTime, Guid, and Random values are not under your direct control. You should abstract them to write better code and testing. We'll see 3 ways to inject and test them in .NET Core projects.
Is string.Format obsolete? Not at all, it still has cards to play! Let's see how we can customize format and create custom formatters.
How to effectively ping an endpoint in C#? Don't use the HttpClient, when .NET provides a Ping class to perform all these operations.
MongoDB is a database based on JSON-like documents, but it can be queried using C#. We'll see how to perform CRUD operations and we'll create some advanced queries.
Wouldn't it be nice if Visual Studio could autogenerate clients for external API? It is actually possible, if they expose an OpenAPI file. Let's see how!
Swagger is a tool that exposes the documentation of your APIs and helps collaborating with other teams. We'll see how to integrate it with .NET Core 3, how to add XML comments and status codes.
Integration tests are useful to check if multiple components fit together well. How can you test your APIs? And how can you mock dependencies?
You're using DateTime.Now, aren't you? Be careful, because it may bring some troubles to your application. Here I'll explain why and I'll talk about time zones and formatting in C# DateTime.
Sometimes on your tests you need to access test files, for example for mocking external data. With manifest resources you can easily reference files for your tests.
BenchmarkDotNet allows you to test the performance on .NET methods. So let's answer a question: is the Enum.HasFlag method really that slow?
Validating inputs is crucial for every application. If you want an easy and versatile way, you can try FluentValidation.
Internal members can be accessed only within the same assembly. And for different assemblies? Here's for you the InternalsVisibleTo attribute!
Singleton, Scoped and Transient: these are the possible lifetimes for DI with .NET Core. How do they change the way objects are constructed?
We've already seen some of the things you should know about enums in C#. Here we'll dive into Flagged enumerations, serialization and so on.
Enums are often used with C#. Boring, right? Maybe there is something you didn't know: casting, parsing and flagged enums.
Asynchronous programming is often seen as something cumbersome, so many developers avoid it. What, why and how to use it? Here are the first steps you can take to approach async programming.
We have already seen how to search for videos in a YouTube channel. Now it's time to get details for a single video.
YouTube provides some APIs for getting info about a channel videos. But they also provide .NET libraries to achieve the same result with C#. Let's have a try!
Extension methods in C# are really useful, but there are few rules to follow...
The CollectionAssert class if fine for basic tests on collections in C#. We will have a look at the methods exposed by this class.
I'm pretty sure that you've already used Guids in C#, but have you ever stopped to think what they are under the hood?an 3 seconds to load. Here you'll learn few trick to improve your site performance.
More than 50% of mobile users abandon a website if it takes more than 3 seconds to load. Here you'll learn few trick to improve your site performance.
On November I shared on Twitter an article each day. They were about C#, general programming and advanced topics. For celebrating the conclusion of this challenge, I wrote a poem about that.
Is it true that the inverse of a negative number is always a positive number? If you think it's true, you might get a subtle error while implementing comparison.
The StringAssert class is a hidden feature of the MSStest framework. Not so many methods, but they can help you with basic tests with strings.
The Assert class is the first step you'll probably take into unit testing. But do you know that there's more than the IsTrue() method?
Some of the things I learned at .NET Conf Italia: interfaces and abstract classes, Blazor, C# default interfaces and .NET Core on Samsung TV
Is a string empty? What if it contains only white spaces? You shouldn't reinvent the wheel, since .NET exposes methods exactly for these cases: String.IsNullOrEmpty and String.IsNullOrWhiteSpace.