Code4IT

Tag – C#

24 posts tagged with "C#" (See all tags)

Understanding Swagger integration in .NET Core

When I write some APIs with .NET Core, one of the very first things I do is to add Swagger. This helps me test my code and helps other developers integrating the APIs I'm exposing. In this article, we're going to add Swagger to an API built with .NET…

How to run integration tests for .NET API

You already write Unit tests, right? But sometimes you want to perform more deep tests, and test not only a small part of your code but the whole execution. In this article, I'm going to explain how to run integration tests on your APIs without…

5 things about DateTime time zones and formatting

Working with dates, if not done carefully, can bring to bugs that can impact your systems. You must always take care of time zones and use the best formatting practices, or else you'll get some trouble. Since I'll talk about time zones, keep in mind…

How to mock dependencies with Manifest resources

It happened that I needed to read a really complex JSON from an external API and elaborate it to adapt the result to my business needs. To test the correctness of those elaborations, I obviously set up a test suite. But the object structure from that…

Enum.HasFlag performance with BenchmarkDotNet

I've recently published two articles about some of the things you probably didn't know about Enums. I had too much to write that I split the original article into 2 parts: here's the first part and here's the second. In both articles I talked about…

Cool validation with FluentValidation

Needless to say, validating inputs is important. Invalid inputs may break your application and bring your system to an unstable state. If you are working with Web API or MVC pages, you are probably used to validation attributes within the definition…

Testing internal members with InternalsVisibleTo

Do you remember the internal access modifier? It allows you to access classes, methods and properties only within files in the same assembly. But sometimes you need to have info about the internal state of that class but within a different assembly…

Dependency Injection lifetimes in .NET - my epiphany

I'm pretty sure you already know what is Dependency Injection (shortened to DI) and why you should implement it in your applications. Just as a recap, DI allows you to define an association between an interface and a concrete class, so that when…

5 more things about enums in C#

In a previous article, I explained some details about enums in C#. Here I'll talk about some other things that are useful and/or curious to know about them. #1: Define enum internal type If you remember, enums can be int, but also short, uint and so…

5 things you didn't know about enums in C#

Enums are really useful if you want to, well, enumerate the possible values for a field. An example of enumeration is the list of movie genres: This way, you can define that a field accepts only values coming from that specific enum, thus narrowing…

First steps with asynchronous programming in C#

Asynchronous programming is a HUGE topic. That's why I'm not gonna talk of everything (even because I'm still learning). I've always seen it as a difficult topic, and I thought it was kind of useless to know and implement it. But I was wrong: it's…

How to get video details from YouTube with .NET Core 3

I have already talked about how to retrieve a list of YouTube videos by its channel ID. Now it's time to check the details of a single video. Let's say that you like a song, you listen to it at least 2 times a day and you want to download its…

How you can create extension methods in C#

Probably you have already heard of extension methods: those are C# methods used to add new functionalities to an existing class. This functionality is available since C# 3.0, so it's largely used and well documented. The basics of extension method…

MSTest CollectionAssert class - an overview

This is the third part of our journey. In the first part we listed the methods belonging to the Assert class, while in the second part we had a look at the StringAssert class. Now we'll deep dive into the CollectionAssert class, that is a good fit…

5 things you didn't know about Guid in C#

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? #1: Guids have a fixed size A GUID is a 128-bit integer (16 bytes) value. That means that there are more than 300,000,000,000,00…

Few tips for performance optimization

When talking about must-have for websites, performance is one of them. Why is it important? And how can you achieve it? 3 seconds are too much Loading time for a web page is crucial, especially in mobile. More than 50% of mobile users abandon a…

A recap of my 30ArticlesForNovember

For this November I decided to share each day an article on Twitter. I did it for simple reasons: share great content with the Twitter community recognize author's work by sharing and promote it discover new things and see the world from a different…

C# sorting - a subtle mistake

Recently I've learned a funny (ehm...) thing. The guilty It isn't true that the inverse of a negative number is a positive number. Or, equally, that (x < 0) => (-x > 0). You could say «Hey, -(-5) == 5». Yes, that's true. We can test it this way: But…

MSTest StringAssert class - an overview

This is the second part of our journey through the Unit Test classes provided with VisualStudio. We already had a look at the class, where had a glimpse of its methods. Now we'll have a look at the StringAssert class, that, as you can imagine…

MSTest Assert class - an overview

You know, Unit Tests are our friends. Usually skipped, often misused, but they are still with us. With or without mocks, they can help us while creating an application, ensuring that the methods we create do what they are supposed to do. In this…

Few things from .NET Conference Italia 2018

Friday, October 26th, 2018 I attended the .NET Conference in Milan, Italy. It was an event organized by ASP Italia, with the sponsorship of Microsoft and Icubed. The conference was about the news in the .NET world: .NET Core 2.x, Azure, C# 8 and…

C# - How to check if a string is really empty

To be, or not to be (empty), that is the question... That's a simple, yet complex, question. First of all, when a string is not empty? For me, when there is at least one character or one number. Do it from scratch Let's create a custom function to…