Tools – Visual Studio Community 2019 and C# 8

In one of my earlier articles, I talked about ways to code for free and the best tools for the job. One of those tools was Visual Studio Community edition, which is a fully featured version of Visual Studio, all for free. At the risk of sounding older than I am, back in the day when I was learning to code, Visual Studio had no free version until Express was released in 2005. To code, you’d have to use an open-source IDE or pay for a full licence, which isn’t easy as a student. To make things worse,
Later, Microsoft replaced the Express version with the Community edition. Which unlike its predecessor, allowed for everything that a paid for version would, as long as you met the criteria for the free version.
At the time of writing the article on free tools for learning to code, the latest version of Visual Studio Community was 2017. In April, Microsoft launched the brand spanking new 2019 version, with a lot of changes both under the hood and for the code that you’re able to write.
So this is clearly more than just a fresh lick of paint. A couple of notable changes that will affect coders of all levels are the improvements to ‘Refactoring’ and introductions of ‘Code Cleanup’ and ‘IntelliCode’.
If you’ve ever used an extension like ReSharper (which I’m going to do an article on soon), then you’ll know just how amazing inline ‘Refactoring’ can be when writing code. At the click of a button, your code can go from sloppy to near perfect. It’s pretty scary how reliant on it you can become if you’re not careful. With the improvements to the built-in refactoring in Visual Studio, you aren’t so dependent on external tools and extensions like ReSharper. VS2019 will immediately show you a number of suggestions for improvements and will immediately apply them once one is selected. This is hugely useful, as it allows you to become familiar with ways of doing things that you might not have previously thought of.
IntelliCode is a new feature, and it uses artificial intelligence built on analysing thousands of Git projects to ‘deliver context-aware code completions’ (I may have pulled that straight from the Microsoft blurb), and can even learn how your team do things, which can guide you in to conforming to a teams coding standards. Finally, like ‘Refactoring’, the introduction of ‘Code Cleanup’ is another important step towards not relying on third party tools. Now, like ReSharper, you can run a full code cleanup based on a custom or predefined profile, at the click of a button against selected code or a full solution.
One of the biggest changes in 2019 is the introduction of C# 8. I try not to be biased towards a particular language on this site, as I understand that not everyone has chosen to learn it as their first language, but it’s the one I’ve used for most of my code examples so regular readings of my content are likely to be using it.
That said, C# 8 brings some fantastic additions to an already fantastic language, and while I’m not going to go into too much detail here about all those features, I’m going to cover one very important addition. Before I do, you might be wondering why you should use these features when you’re still trying to get to grips with the basics of the language, and for the most part, you’re entirely correct. Not only is it unlikely that you’ll even use these new features for a while, but reading about some of them might only confuse you further if you’re currently struggling with the earlier implementation of it.
However, for some features, using these now will be a huge advantage to you to learn the new way of doing things rather than learn the old way first.
The most talked about and most welcomed feature of C# 8 is nullable reference types. If those three words just flew straight over your head, don’t worry about it. The importance of this feature is understanding how it will improve your code and reduce NullReferenceException errors, which you will learn to loathe in your coding journey. So how do we use it?
Consider the following code:
namespace CSharpEight { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public Person(string firstName) { FirstName = firstName; } } public class Program { static void Main(string[] args) { var person = new Person("Toby"); Console.WriteLine($"Hi {person.FirstName}, {person.LastName}") } } }
With the above code, we’ll be able to compile it with no warnings or errors at all. The second we run it
In C# 8, this problem goes away due to the fact that reference types, such as string, become non-nullable. This would be an absolute nightmare for some large projects to use as a lot of code would suddenly need to be refactored to build and run, so to avoid this issue, null value reference types are shown as
To use this new feature, we have to first enable it:
#nullable enable namespace CSharpEight
It’s as simple as that. Now, if we were to build the exact same code as the first example, we’d be presented with a compiler warning, saying “Non-nullable property ‘LastName’ is uninitialised”. To make this warning go away, we simply have to initialise LastName in our constructor, like we are already doing with FirstName, and now that warning goes away. You can imagine just how many thousands of warnings larger, commercial projects are going to see when enabling this, but due to them being warnings, the code refactoring can be phased in slowly as opposed to being forced in one go.
You might wonder why I covered this feature of all the new ones available in C#8? Well truth be told, I am actually going to write an article covering some of them in a new series called ‘Then and Now’ (I’ll hopefully think of a less cheesy name before they are published), showing examples of how a particular code was written before C#8 and how it is written since its introduction. But going back to why I chose nullable reference types, I really feel that this feature can massively help new coders especially, learn the importance of tidy and well-written code. Knowing how to write code which is safeguarded against NullReferenceExceptions is very important, and now, having the compiler warn you each time you’re doing it, means you can write better code as you go instead of having to deal with it later.
If you do want to read about the other new features in the
I think the take home from this article should be that given this is a completely free tool, it absolutely should be on your list of things to do. Whether you want to benefit from the clever additions to Visual Studio itself, or you just want to start writing the latest code with C#8, downloading Visual Studio Community 2019 today makes a whole lot of sense. You can download Visual Studio 2019 from the following link