Javatpoint Logo
Javatpoint Logo

C# Nameof Operator

C# NameOf operator is used to get name of a variable, class or method. It returns a simple string as a result.

In error prone code, it is useful to capture a method name, in which error occurred.

We can use it for logging, validating parameters, checking events etc.

Note: if we want to get fully qualified name, we can use typeof expression along with nameof operator.

Let's see an example that implements nameof operator.

C# Nameof Operator Example 1

Output:

Variable name is: name
Method name is: show

We can also use it to get method name in which exception is occurred. See, the following example.

C# Nameof Operator Example 2

Output:

Index was outside the bounds of the array.
Method name is: show





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA