Skip to content

Blog

Use the required Keyword in C# to Enforce Property Initialisation

Last week we saw how much code we can get rid of when we use the [init keyword to create immutable objects]. The only downside we found was that we cannot enforce that all properties get a value. To solve this problem, we can use the required keyword of C# 11:

The required modifier indicates that the field or property it is applied to must be initialized by an object initializer. Any expression that initializes a new instance of the type must initialize all required members. – from learn.microsoft.com

The init Keyword for Immutable Properties in C

With C# 9 we got the new init keyword. You may have heard of it when it was introduced, but probably not used it much at the time. Then as with so many new things in the C# language, the new syntax did not offer new functionality but syntactic sugar to get the same results faster. Why should we care now? Because in combination with other newer features in C# that init keyword is a great help.

What was the init keyword again?

The init keyword defines an accessor method in a property or indexer. An init-only setter assigns a value to the property or the indexer element only during object construction. An init enforces immutability, so that once the object is initialized, it can't be changed. – from learn.microsoft.com

Little Git Tricks: Revert Changes in a File

If you want to revert a change in a file with Git, you can use this command:

git checkout -- ./path/to/file
The double-hyphen (--) tells Git to take what follows as its second argument (the path to the file). This makes it clear that you did not specify a branch.

While this works, it is not ideal. You might wonder what command to use or where the -- should go. It can be hard to remember and we often need to check the documentation to make sure we do it the right way. Is there a better way?

5 Things to Test With List Parameters

Whenever we have a method that accepts a list as a parameter, we should test these 5 cases to see if the method handles the input correctly:

  1. Null
  2. Empty
  3. One
  4. A few
  5. Many

Let us take a closer look at each of those inputs and what they can check for us. The list is not my invention, but so far, I was unable to attribute it correctly. If you know who the source was, please let me know.

How to Fix the Multiple Entry Points Error in .Net Applications

Before I start making changes, I usually run the template code to see if everything works so far. Even when it only has the template code, problems could happen. A few weeks ago, I skipped this step and after I made my changes, I got this error:

Error (active) CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.

Awesome Awesomeness List

When we search for a library or a package, we usually have too many options. Which one should we choose? What are good approaches? The less we know about the specific topic, the less we can make a good decision.

That is where awesome lists come to play. They are curated lists by individual developers or community members that selected resources they found especially helpful. That can be packages, blog posts, tutorials, or anything else.

Little Git Tricks: Fix Unexpected Disconnect on Sideband Packet

I run into a strange error while trying to clone a Git repository from Azure DevOps on the command line:

Read from remote host vs-ssh.visualstudio.com: Connection reset by peer
client_loop: send disconnect: Connection reset by peer
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output