Why you should give a second look at C# and .NET

When C# and the .NET Framework were first introduced in the early 2000’s, the platform looked like a cheap knockoff from Microsoft of the Java ecosystem. C# at the time was essentially a “Java for Windows” language and could only be run on Windows machines. The .NET Framework was also a piece of technology that could only be run on Windows. For awhile, the platform wasn’t seen as a legitimate development platform.

Fast forward to 2020, and that ecosystem is all but gone, replaced by something far greater and more promising for the future. According to the 2019 Stack Overflow Developers Survey, where C# is number 7 on most popular technologies and number 9 on the most loved languages. .NET Core is number 3 in the most popular technologies and is most loved for other tools and frameworks and number 5 on most wanted.

So what happened? In 2014, Microsoft announced it’s new platform, .NET Core, would be open source and developed alongside the .NET Framework. Before then, if you wanted to write C# code on any system other than Windows, you had to use Mono. There was also an open source tool called Xamarin, which is used develop iOS and Android applications in C#. In 2016, Microsoft acquired Xamarin and today is sponsoring the Mono project. .NET Core is currently on version 3.1 and C# on version 8. C# 9 will be coming out soon and .NET Core and Frameworks are being merged into on project, known as .NET 5.

Those are some pretty bold moves to make from a company that once openly despised open source software and actively tried to compete against it. Between this, the Windows Subsystem for Linux, the new terminal, the embracing of React Native, and more, it looks like the software team at Microsoft looks like it’s done a complete turn around on it’s old stances and is trying to appeal to more developers. If you previously avoided or despised working C# or .NET, it might be time to take another look.

C# is Awesome

It is true that once, C# was seen as nothing more than a Java clone. That cannot be said in 2020. C# has its own identity now that makes it stand out from Java. Not only does it stand out, C# is more pleasant to write in and does not require the same kind of boiler plate code Java does. Here a few of my favorite features of the language:

Properties

public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }

You can see in the example above that I was able to declare three variables with, essentially, getters and setters. In Java, this would have been at least 15 lines of code, 3 for the private variable declarations and 12 for writing out the getters and setters. You also don’t necessarily need a constructor with this syntax either.

Need immutability? C# 9 is introducing a new syntax that allows you to declare a variable immutable and only assign it’s values at creation.

public string ProductName { get; set; }
public int ProductCategoryId { get; init; }

This now allows you to create new products and have properties be easily immutable.

LINQ and var

LINQ is wonderful. LINQ stands for Language Integrated Query and it at first glance, it may seem like just an attempt to write SQL in the language, but it’s far more than that. Combined with the var keyword, we can make magic happen.

Say I have an in memory list of locations that I am visiting on a road trip. After I visit each one, I set a variable called “Visited” to true on that object in my list.

If I want to get all the places I have been or have not been, I can do this:

public List<Place> places = new List<Place>();// Objects are added to the list// Lambda syntax
var placesToVisit = places.Where(p => p.Visited == false).ToList();
// Query syntax
var placesToVisit =
from p in Places
where p.Visited == false
select p;

You can see that there are two ways to write LINQ queries, a Lambda way and a Query way that looks like SQL. I tend to prefer the Lambda way, but it’s just a preference. The var keyword is really hand here as well, especially if you don’t know what kind of return type you will get back. I want to make this point though, var does NOT mean the object does not have a type. It simply has the compiler figure out what it’s supposed to be.

var words = "Hello there!";// This is valid
words = "General Kenobi!";
// This is NOT valid
word = 7;

In the example above, you can see that var is expecting to be a string. If this was a dynamic language like Javascript or Python, word = 7 would be allowed. In C# it isn’t, because C# is a statically typed language.

Async/Await

C# has wonderful support for asynchronous programming. There are a couple ways to accomplish it and the examples I will use are straight from the Microsoft docs because I think they are short and to the point.

The first example is showing how to download data without blocking the UI thread.

private readonly HttpClient _httpClient = new HttpClient();  downloadButton.Clicked += async (o, e) => {     
// This line will yield control to the UI as the request
// from the web service is happening.
//
// The UI thread is now free to perform other work.
var stringData = await _httpClient.GetStringAsync(URL);
DoSomethingWithData(stringData);
};

This I/O bound example is great for things like downloading data from the internet or getting results back from a database.

On the other side, we have CPU bound operations. Here, we can take an expensive task and run it in the background. Here is that example from the Microsoft docs.

private DamageResult CalculateDamageDone()
{
// Code omitted:
//
// Does an expensive calculation and returns
// the result of that calculation.
}
calculateButton.Clicked += async (o, e) =>
{
// This line will yield control to the UI while
// CalculateDamageDone() performs its work. The UI thread is
// free to perform other work.
var damageResult = await Task.Run(() => CalculateDamageDone());
DisplayDamage(damageResult);
};

As you can see, it is very easy to get going with asynchronous operations in C#.

.NET is a Wonderful Platform

.NET has really come along way. With the move to .NET 5 and having a unified development platform, there is a lot be excited about for the future. Here are just a few of my favorite parts about working in the .NET ecosystem.

The Editors are top Notch

Where I do I start with the variety of editors you can develop in? There are a lot of really awesome IDE’s and text editors you can use to build apps in .NET, the biggest ones being:

  • Visual Studio (Windows; IDE)
  • Visual Studio for Mac (MacOS; IDE)
  • Visual Studio Code (Windows, Mac, Linux; Text Editor)
  • Rider (Windows, Mac, Linux; IDE)

I personally use all the Visual Studio products for .NET development. They make it really easy to get up and going with developing .NET applications. Visual Studio for Windows in particular is an absolute monster of an editor that has a lot of great tooling and configuration built into it.

The CLI and Package Management tools are very powerful

I have a soft spot for CLI’s and .NET’s delivers. It’s super easy to make a new solution, add projects to it, setup project secrets, run tests, build your apps, and much more. The CLI is available for all platforms that .NET Core is supported on.

With package management, .NET uses the NuGet package manager, which makes is really easy to add and manage dependencies to your project. To install a new dependency, all you need to type is

dotnet add package <package name>

In Visual Studio, you don’t even need to use the CLI to add dependencies, although you can if you want. Visual Studio’s built in NuGet management window makes it super easy to search and manage your packages.

Getting a project up and running is really, really easy with these tools.

Entity Framework Makes Database Migrations a Breeze

If you have used Django, you know it has a built in ORM. For Java you have Hibernate and Javascript you have more than plenty of options for an ORM. .NET’s ORM is Entity Framework and it makes it very, very easy to map out your entities and get your database updated.

Say I have two models, one for authors and one for books. An author can have multiple books and books can have multiple authors. This is a many-to-many relationship and is usually not fun to map out in your code. Entity Framework makes this much more bearable.

// Model creation omittedpublic class BookstoreContext : DBContext {
public BookstoreContext() : Base() { }

public DbSet<Authors> { get; set; }
public DbSet<Books> { get; set; }

public override void OnModelCreating(
DbModelBuilder modelBuilder) {
modelBuilder.Entity<Author>()
.HasMany<Book>(a => a.Books)
.WithMany(b => b.Authors)
.Map(ab =>
{
ab.MapLeftKey("AuthorId");
ab.MapRightKey("BookId");
ab.ToTable("AuthorBook");
});
}}

As you can see, it is really straightforward to create complex relationships in Entity Framework. When you migrate this to your database, you will have a Author table, Book table, and AuthorBook table.

Microsoft has really come along way in terms of tools and support for developers. C# is now one of the most loved languages and is very easy to learn and powerful to use. The .NET platform continues making forward strides as well, now that is open source and cross platform.

As .NET continues to grow, it’s already wide array of use cases will grow as well. You can build games in Unity and Godot, you can build web apps, desktop applications, micro-services, IoT applications, AI and machine learning, and the list goes on.

If you were somebody who once despised this platform, it might be worth another look now. With the amount of tooling and cross platform support, .NET in 2020 is nothing like it was years ago. It’s grown and evolved into a tool that everybody can use. And if you are just starting out in software development, C# is a wonderful language to learn and develop in. You can find all the documentation on how to get setup and going on Microsofts website.

With this new direction that .NET is taking and how powerful C# has become, there isn’t a better time to jump into the .NET ecosystem.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brock Joseph Herion

Brock Joseph Herion

I am a software developer who love coding in Python, Javascript, and C#. I am a sucker for learning new technologies and tooling to make development easier.