Sign in


If you’re anything like me, you’ve looked for ways of making your applications faster. In this article, I’ll show you a somewhat unknown class in the .NET framework that makes lazy creation of objects easy to do and thread safe.

The Old Way

Before we get to the fun stuff, let’s take a look at how you might try to do these things without .NET’s Lazy<T> classes.

Typically when you design a class, you might start with code like this:

You might eventually find that as the number of things you need to instantiate grows, the performance degrades significantly on creation of the…

Continuing from the previous part, where we’ve written and tested our parser using Antlr 4 — let’s now generate a C# parser, and hook it up to search an elasticsearch index.

Building the C# Parser

antlr4 -Dlanguage=CSharp -o Generated Predicate.g4 -no-listener -visitor

You will need to include Antlr4.Runtime.Standard as a dependency to use the generated C# code.

Adding Some Extra Abstraction

I keep the surface area of interfaces at the minimum. This gives me a few benefits:

  1. The first obvious benefit is simplicity — I don’t expose things that will never be used, this keeps the solution simple making it easier to understand and use.
  2. This somewhat decouples…

For some products, having your own language that your customers can use to be productive, or make your product that much more powerful, is a Domain Specific Language. Think Atlassian’s Jira Query Language, Github’s filter feature etc.

Why not hand-roll your own parser? — well, in 99% of cases, you wouldn’t really need the extra granular control to squeeze out every bit of performance out of a parser, or nor will you need extra features that Antlr would not be able to provide — these types of requirements are usually reserved for products that are sold as a language themselves…

There are processes you want to run frequently and automatically in the background of your main application. There are many libraries dedicated to this problem, and Quartz.Net is one of the known ones out there.

In this post, I’m going to create a .NET Core console app that will generate a random number on the console output, every 15 seconds.

The first step will be installing the latest stable Quartz NuGet package on your project

Create the Job Class

I’m going to create a job that will generate a 4 digit random number and print it on the console. The class needs to implement IJob interface which lives in Quartz namespace


using Quartz; using System; using System.Threading.Tasks; namespace…

In this post I have shown how you can use an IActionFilter in ASP.NET Core MVC to read the method parameters for an action method before it executes. I'll show two different approaches to solve the problem, depending on your requirements.

In the first approach, you know that the parameter you’re interested in (a string parameter called returnUrl for this post) is always passed as a top level argument to the action, e.g.

public class AccountController
public IActionResult Login(string returnUrl)
return View();

In the second approach, you know that the returnUrl parameter will be in the…

Where to Put a Call to Task.Run

Let’s pick up where we left off with the application from the previous guide. That application downloaded an image and then blurred it using a library called ImageSharp (available in NuGet as SixLabors.ImageSharp). We defined a method called BlurImage as follows:

static async Task<byte[]> BlurImage(string imagePath)
return await Task.Run(() =>
var image = Image.Load(imagePath);
image.Mutate(ctx => ctx.GaussianBlur());
using (var memoryStream = new MemoryStream())
return memoryStream.ToArray();

Notice that the call to Task.Run is immediately before the image processing code. Is that the best approach?

Whether for convenience or clarity, you might find yourself putting…


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