Safer C# with the nameof operator
Tags: csharp, dotnet, codequality
Published: 2019-10-29 05:31:01

---
title: Safer C# with the nameof operator
published: true
date: 2019-10-29 05:25:37 UTC
series: .NET Quality
tags: csharp, dotnet, codequality
cover_image: https://res.cloudinary.com/practicaldev/image/fetch/s--H5nXBbo6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/killalldefects.com/wp-content/uploads/2019/10/NPCNameof.png%3Fw%3D770%26ssl%3D1
canonical_url: https://killalldefects.com/2019/10/29/safer-csharp-with-the-nameof-operator/
---

C# 6 introduced a simple operator that can prevent several types of issues if properly used. In this short article, we’ll explore the nameof operator and how it can improve maintainability while reducing defects.

## Introducing the nameof operator

The `nameof` operator substitutes the name of a named member at _compile time_.

Let’s take a look at some WPF code that could benefit from this operator:

![The old notify property changed pattern without using nameof.](https://i2.wp.com/killalldefects.com/wp-content/uploads/2019/10/NPCOld.png?w=770&ssl=1)

This example exposes a property changed event as part of implementing `INotifyPropertyChanged`. Because of that, it’s important that the property name match the string passed in to `OnPropertyChanged`. If these don’t match up well, WPF and other XAML-based frameworks will not properly refresh the UI.

Instead of hard-coding a string name here, we can make a small change and use the `nameof` operator like so:

![Notify Property Changed using the C# 6 nameof operator.](https://i1.wp.com/killalldefects.com/wp-content/uploads/2019/10/NPCNameof.png?w=770&ssl=1)

The way this works, is the compiler evaluates the `nameof` operator at _compile time_ and substitutes the name of the member being referenced into the generated binary. This means that the end result is going to be the same as the earlier example.

_Note: if you’re using XAML-based technologies, I recommend you also consider using [caller member info](https://killalldefects.com/2019/10/15/caller-member-info-c-and-you/) for property changed notifications._

## Okay, so how is this useful?

If `nameof` can’t resolve the name of the member you’re referencing, you will get a compiler error and be forced to resolve it. That means that if you misspell the name of a member or you change a method name later that `nameof` pointed to, you’ll know about it as soon as you try to build.

This means that when you use the nameof operator, you’re no longer in danger of using a misspelled or out-of-date string.

Okay, I get it. You probably don’t do any XAML-based development. Fine.

I bet you do at least occasionally perform argument validation.

Code like the following is usually common in codebases:

![Argument validation without the nameof operator](https://i1.wp.com/killalldefects.com/wp-content/uploads/2019/10/ArgumentValidation.png?fit=770%2C229&ssl=1)

Developers don’t usually think to search for string references when renaming parameters to methods, so you sometimes see incorrect or misspelled parameter names provided to the `ArgumentException` family of exceptions.

In fact, code like this is open copied and pasted without thought because it can tend to be boilerplate logic. Because of this, it’s not extremely uncommon to see the wrong parameter name entirely being provided to the exception. That’s a recipe for debugging pain.

The nameof operator is a perfect solution to this problem:

![Argument validation using the nameof operator](https://i0.wp.com/killalldefects.com/wp-content/uploads/2019/10/ArgumentValidationNameof.png?fit=770%2C213&ssl=1)

* * *

Nameof is not going to revolutionize the way you write code, but it is likely to improve the maintainability and accuracy of the code – at least a little bit.

If you’d like to learn more, I recommend starting with [Microsoft’s documentation](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof).

Every little thing helps, and so I recommend you keep the nameof operator in mind when writing code in C#.

The post [Safer C# with the nameof operator](https://killalldefects.com/2019/10/29/safer-csharp-with-the-nameof-operator/) appeared first on [Kill All Defects](https://killalldefects.com).