The nameof keyword in C#

The nameof operator in C# returns the string literal name of a variable, type, or member. It provides a compile-time constant string that represents the name of the code element, making it useful for logging, exception messages, and property change notifications without hardcoding strings.

Syntax

Following is the syntax for using the nameof operator −

string name = nameof(element);

Where element can be a variable, property, method, class, or namespace.

Using nameof with Variables

The nameof operator returns the variable name as a string, which is resolved at compile time −

Example

using System;

public class Program {
   static void Main() {
      var vehicle = "motorbike";
      Console.WriteLine(nameof(vehicle));

      var time = DateTime.Now.ToLocalTime();
      Console.WriteLine(nameof(time));

      var isActive = false;
      Console.WriteLine(nameof(isActive));
   }
}

The output of the above code is −

vehicle
time
isActive

Using nameof with Types and Members

The nameof operator can also be used with class names, method names, and property names −

Example

using System;

class Student {
   public string Name { get; set; }
   public int Age { get; set; }
   
   public void DisplayInfo() {
      Console.WriteLine("Student information displayed");
   }
}

public class Program {
   static void Main() {
      Console.WriteLine(nameof(Student));
      Console.WriteLine(nameof(Student.Name));
      Console.WriteLine(nameof(Student.Age));
      Console.WriteLine(nameof(Student.DisplayInfo));
      Console.WriteLine(nameof(Console.WriteLine));
   }
}

The output of the above code is −

Student
Name
Age
DisplayInfo
WriteLine

Common Use Cases

The nameof operator is particularly useful for exception handling and argument validation −

Example

using System;

class Calculator {
   public static double Divide(double numerator, double denominator) {
      if (denominator == 0) {
         throw new ArgumentException($"Parameter {nameof(denominator)} cannot be zero");
      }
      return numerator / denominator;
   }
   
   public static void LogOperation(string operation, double result) {
      Console.WriteLine($"Operation: {nameof(operation)} = {operation}, Result: {result}");
   }
}

public class Program {
   static void Main() {
      try {
         double result = Calculator.Divide(10, 2);
         Calculator.LogOperation("Division", result);
         
         Calculator.Divide(10, 0);
      }
      catch (ArgumentException ex) {
         Console.WriteLine("Error: " + ex.Message);
      }
   }
}

The output of the above code is −

Operation: operation = Division, Result: 5
Error: Parameter denominator cannot be zero

Benefits of Using nameof

  • Compile-time safety − If you rename a variable or method, the compiler will update the nameof expression automatically.

  • Refactoring support − IDEs can safely rename elements without breaking string references.

  • Performance − The string is resolved at compile time, not runtime.

  • Maintainability − Eliminates hardcoded strings that can become outdated.

Conclusion

The nameof operator provides a safe, compile-time way to get string representations of code elements. It is especially valuable for exception messages, logging, and property change notifications where you need the actual name of variables, types, or members as strings.

Updated on: 2026-03-17T07:04:35+05:30

610 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements