I’ve done mostly Python and C++ for my software development career. I used C++ in constrained environments where clock cycles mattered, and Python when we needed to iterate quickly and be productive. They felt like different ends of the spectrum, and I learned what I liked and didn’t like about both languages.

Python Likes:

  • Flexible
  • Productive
  • Standard library
  • Lots of good libraries on Pypi

Python Dislikes:

  • Environment management
  • Not efficient
  • You have to accept dynamic typing

C++ Likes:

  • Speed and efficiency
  • STL - good C++ is elegant

C++ Dislikes:

  • CMAKE
  • Undefined behavior
  • Older code - bad C++ gives Perl a run for its money.
  • Memory safety
  • Slower productivity

In the past couple of months I’ve taken a look at new technologies and languages. There were two languages I’ve reviewed in some depth; Java and C#. Java was more of a review, and to learn more about Apache Camel. With C#, I looked at the ecosystem and saw a new framework called Blazor that I picked up a course and saw its capabilities.

What’s the backstory here?

We’re all familiar with how Java grew out of the vision to address some of the drawbacks of using C++. And we’re also familiar with how Microsoft sought to compete with Java by writing J++ and getting its hands smacked. C# was then developed to be similar to Java, but not too similar.

Because I loved Python so much at the time I was learning software development, I ignored C#. I dabbled in it from time to time, making examples of WPF apps. I didn’t really go further than that. C# was OK, but there were some limitations and verbosity that I didn’t enjoy.

Also back then .net wasn’t open source, and just didn’t have the mac and linux story it has today.

What is Blazor?

In a nutshell, Blazor allows you to program the front-end and back-end in C#. Instead of going the Node.js route, you push the backend to the front end. There are a few approaches you can use with Blazor.

  • Blazor server app
  • Blazor webapp

The web app uses web assembly to compile the C# code into low level JS. I haven’t tried that yet. I’m using Blazor server app, which means a client on the front end talks to the server through a websocket. Essentially the server is controlling the front end like a puppetmaster.

Why is it so great?

There are two primary reasons why I think Blazor is great.

  1. I’m not a fan of javascript.
  2. I like solutions where you can write in a single language.

Everything else is ancillary - but still good.

C# feels like the C++ I kind of always wanted. It has matured to the point where it doesn’t feel nearly as verbose as it once did so many years ago.

public class Foo
{
    public Foo(int value1, int value2)
    {
        this.value1 = value1;
        this.value2 = value2;
    }

    public int value1 { get; init; }
    public int value2 { get; init; }

    public (int, int) GetValues() => (value1, value2);
}

Can be used like…

var f = new Foo(42, 99);
var (a, b) = f.GetValues();
Assert.That(a, Is.EqualTo(42));

That’s honestly not that bad, this is easier to use than the C# of the 2000’s.
I know this is only a brief example, but it gets the point across. Everyone wants static type checking with flexibility, and I think C# is there now.

Another benefit is the tooling and packaging. Python, Javascript, Java, and C++ all have their issues when it comes to packaging for production. The Rider IDE I’ve been using makes this aspect of programming much simpler (yes, I know it’s hiding the intricacies from me).

So here’s the demo code from the Blazor initial skeleton…

@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }

}

If I squint hard enough - that looks a lot like Vue.js. I like Vue.js. It just feels like I’m working with React or Vue.js where I can call my business logic directly rather than going through an axios call.

What’s not so great about Blazor?

Right now Blazor is trying to stabilize on a solution that will provide good first loading experience and then good performance thereafter. With the version of Blazor I’m using either you use the websocket tech - which means it doesn’t let you go to google scale. If you use web assembly, that means a somewhat hefty download at the start.

Neither of these issues is a problem for smaller companies, but Blazor is working on uniting the two to give you the best of both worlds.

Secondly - Because you can put your UI code and the business logic in the same place - it doesn’t mean you should. If I ever use this technology in production, I will be sure to enforce separation of concerns. I would still try to write the C# business logic such that the UI could be replaced in the future with a React front end, or even a GUI front end.

Mixing UI and backend just means complecting the two - and that’s just a bad recipe.

Also, you’re going to get a slight hit with your interaction with JS libraries that make your site pretty. You can still use the libraries through interop - It’s just not seamless, and it’s little clunky. One could argue that you should be more focused on the steak and not the sizzle, but it’s a consideration nonetheless.

Finally - we’re talking Microsoft here. Blazor is the new tech on the block, and the older web tech MS has previously promoted is now not focused on. That’s true for every community, but eventually Blazor will be replaced by something else, and you’ll need to plan for that.

Where would I use C# and Blazor?

I don’t think Blazor replaces React + API go-to solution the industry has come to adopt… yet. I also think that Django is still a great solution for backend system management and making rest APIs super-quick. Blazor’s sweet spot is the long-tail of web applications that are not in the top 20% of websites, but where Django just isn’t enough. It’s a good tool to keep engineering teams smaller, as engineers can do front and back end.

Conclusion

In Summary - I think trying out Blazor in a Udemy course is worth your time. If your having trouble getting a front end team in place for your product, this tool would let your backend team work the front end. C# as a language has gotten flexible enough that dynamic language users might actually like it.