C# – The nameof() operator

The nameof() operator outputs the name of the class/method/property/type passed into it. Here’s an example:

Console.WriteLine($"Variable name: {nameof(movie)}"); //Variable name: movie

Console.WriteLine($"Type name: {nameof(Movie)}"); //Type name: Movie

Console.WriteLine($"Field name: {nameof(movie.RuntimeMinutes)}"); //Field name: RuntimeMinutes

Console.WriteLine($"Method name: {nameof(Program.Main)}"); //Method name: Main
Code language: C# (cs)

Note: nameof() was added in C# 6.

nameof() eliminates duplication

The DRY principle – Don’t Repeat Yourself – warns us against having duplication in the code. Whenever information or code is duplicated, it’s possible to change something in one spot but forget to change it in another spot, which can lead to bugs.

Without nameof(), you’d have to use a hardcoded string literal to represent the name of something. This means the information (the name of the thing) is duplicated in multiple places. The nameof() operator eliminates the duplicate information and therefore helps us to adhere to the DRY principle.

For example, let’s say you have the following method:

static void ShowMovie(Movie movie)
{
	if (movie == null)
		throw new ArgumentNullException("movie");

	//show movie
}
Code language: C# (cs)

The name of the movie parameter is duplicated in two spots: the parameter itself and the hardcoded “movie” string being passed into ArgumentNullException.

Now let’s say you change the name of the movie parameter to pelicula, but forget to change the name of the parameter being passed into ArgumentNullException:

static void ShowMovie(Movie pelicula)
{
	if (pelicula == null)
		throw new ArgumentNullException("movie");

	//show movie
}
Code language: C# (cs)

Let’s go back to the beginning and use nameof(movie) instead of hardcoding “movie”:

static void ShowMovie(Movie movie)
{
	if (movie == null)
		throw new ArgumentNullException(nameof(movie));

	//show movie
}
Code language: C# (cs)

Now rename the movie parameter to pelicula in all spots where it’s used:

static void ShowMovie(Movie pelicula)
{
	if (pelicula == null)
		throw new ArgumentNullException(nameof(pelicula));

	//show movie
}
Code language: C# (cs)

If you’re using the rename refactoring shortcut, it will automatically rename nameof(movie) to nameof(pelicula). If you’re manually renaming the parameter, and forget to change nameof(movie) to nameof(pelicula), then you’d get a compiler error because the movie parameter doesn’t exist.

In both cases, nameof(movie) would end up getting renamed to nameof(pelicula), thus avoiding the bug.

This is one of the primary benefits of using nameof(). It eliminates information duplication so you can avoid bugs and not have to hardcode variable names.

Other examples of using nameof()

There are many practical uses of nameof(). Here are just a few examples.

Setting a constant value to the name of the constant

If you need the value of a constant to match its name, the simplest way to do that is to use nameof() like this:

//using nameof()
public const string EndpointPort = nameof(EndpointPort);

//instead of hardcoding the constant value
public const string EndpointPort = "EndpointPort";
Code language: C# (cs)

Using nameof() to specify column names in a SQL query

When you have a model class with property names that match the column names in the table, you can use nameof() when executing raw SQL queries in the code. This way if the column name changes, and you change the property in the model, it will automatically update in the queries too:

//using nameof()
using(var connection = new SqlConnection(ConnectionString))
{
	return connection.Query<Movie>($"SELECT {nameof(Movie.Name)}, {nameof(Movie.Description)} FROM Movies");
}

//instead of hardcoding the column names
using(var connection = new SqlConnection(ConnectionString))
{
	return connection.Query<Movie>($"SELECT Name, Description FROM Movies");
}
Code language: C# (cs)

Using nameof() instead of typeof().Name

When you use nameof() on a type, it outputs the type name. In other words, you can use nameof() instead of typeof().Name, which saves a little bit of typing.

nameof(Movie)

typeof(Movie).Name
Code language: C# (cs)

Leave a Comment