Convert DateTime to user's time zone with Blazor in .NET 8
Meziantou's blog
by Gérald Barré
2d ago
This post is an update of the original post Convert DateTime to user's time zone with server-side Blazor to take advantage of new .NET 8 features. It is inspired by the following pull request in dotnet/aspire When you display DateTime data to a user, you may want to convert the value to the user's time zone. With server-side Blazor, the code is executed on the server, so DateTime.Now corresponds to the ..read more
Visit website
Generate OpenAPI specification at build time from the code in ASP.NET Core
Meziantou's blog
by Gérald Barré
1w ago
The OpenAPI specification is a powerful tool to describe and document APIs. It is a standard that allows you to define the structure of your API, including the endpoints, the request and response models, and the security requirements. The OpenAPI specification is a JSON or YAML file that can be used to generate documentation, client libraries, and server stubs. Most .NET developers generate the ..read more
Visit website
Set a blank page for new tabs in Microsoft Edge
Meziantou's blog
by Gérald Barré
2w ago
Microsoft Edge can be configured using the setting pages edge://settings. However, some settings are not available in the UI. For instance, you cannot replace the new tab page. You can only configure the content of the page... So, you have to stick with the Edge-like page with news and other stuff you don't care about. Hopefully, you can configure Microsoft Edge using policies. There are multiple ways to ..read more
Visit website
Optional parameters may appear in the middle of the parameter list
Meziantou's blog
by Gérald Barré
3w ago
In .NET, optional parameters are not always the last parameters. While C# does not allow the declaration of optional parameters in the middle of the parameter list, it is possible to do so in IL or from other languages like VB.NET or F#. Also, the compiler may lower some features and create methods with optional parameters in the middle of the parameter list. Why does it matter? If you write a source ..read more
Visit website
Creating an HttpClient that uses DNS over Https
Meziantou's blog
by Gérald Barré
1M ago
DNS is a key component of the Internet. It's used to translate a domain name to an IP address. For instance, when you type https://www.meziantou.net in your browser, the browser will query the DNS server to get the IP address of the server hosting the website. Then, the browser will connect to the server using the IP address. A good practice is to rely on the OS configuration to query the DNS server ..read more
Visit website
Enable the new TerminalLogger in .NET 8 SDK automatically
Meziantou's blog
by Gérald Barré
1M ago
In .NET 8, you can use the new TerminalLogger. This terminal logger provides better output than the default console logger. It provides live progression and improves error reporting. However, it's not enabled by default. You need to use the --tl option to enable it (e.g. dotnet build --tl). Instead of adding the flag every time you run a dotnet command, you can enable the new terminal logger by adding the ..read more
Visit website
Checking if a collection is empty in C#
Meziantou's blog
by Gérald Barré
2M ago
In C#, there are different ways to check if a collection is empty. Depending on the type of collection, you can check the Length, Count or IsEmpty property. Or you can use the Enumerable.Any() extension method. // array: Length int[] array = ...; var isEmpty = array.Length == 0; // List: Count List<int> list = ...; var isEmpty = list.Count == 0; // ImmutableArray<T: IsEmpty ImmutableArray<int> ..read more
Visit website
How to get assembly code generated by the JIT for a C# method
Meziantou's blog
by Gérald Barré
2M ago
When you execute a .NET method, the JIT compiles the method to native code. This native code is then executed by the CPU. In this post, I describe how you can inspect the generated assembly code. Using the DOTNET_JitDisasm environment variable Starting with .NET 7, you don't need any complex tools to inspect the generated assembly code. You can use the new environment variable DOTNET_JitDisasm with the ..read more
Visit website
Difference between CultureInfo.Get and new CultureInfo
Meziantou's blog
by Gérald Barré
2M ago
When you want to get a CultureInfo object, you can use the static method CultureInfo.GetCultureInfo or the constructor of the CultureInfo class. In this post, I describe the difference between CultureInfo.GetCultureInfo and the constructor of the CultureInfo class. var cultureInfo1 = CultureInfo.GetCultureInfo("en-US"); var cultureInfo2 = new CultureInfo("en-US"); The constructor creates a new instance ..read more
Visit website
Making primary constructor parameters read-only
Meziantou's blog
by Gérald Barré
2M ago
C# 12 introduced a new feature called Primary constructor. This feature allows us to define a constructor directly in the class declaration. // You can define a constructor directly in the class declaration. public readonly struct Distance(double dx, double dy) { public readonly double Magnitude { get; } = Math.Sqrt(dx * dx + dy * dy); public readonly double Direction { get; } = Math.Atan2(dy, dx); } This ..read more
Visit website

Follow Meziantou's blog on FeedSpot

Continue with Google
Continue with Apple
OR