Questions tagged [nameof]

In C#, `nameof` expressions are a form of reflection. They return the string representation of the argument, as seen by the compiler.

What is the purpose of nameof?

100 questions
365
votes
17 answers

What is the purpose of nameof?

Version 6.0 got a new feature of nameof, but I can't understand the purpose of it, as it just takes the variable name and changes it to a string on compilation. I thought it might have some purpose when using but when I try to nameof(T) it just…
atikot
  • 4,761
  • 4
  • 26
  • 34
58
votes
4 answers

C# nameof generic type without specifying type

Assume I have the type public class A { } and somewhere in the code I want to throw an exception related to incorrect usage of that type: throw new InvalidOperationException("Cannot use A like that."); So far, so good, but I don't want to…
Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
53
votes
4 answers

Using nameof to get name of current method

Have browsed, searched and hoped but cannot find a straight answer. Is there anyway in C# 6.0 to get the current method name using nameof withouth specifying the method name? I am adding my test results to a dictionary like…
Marcus
  • 8,230
  • 11
  • 61
  • 88
50
votes
9 answers

How to use C# nameof() with ASP.NET MVC Url.Action

Is there a recommended way to use the new nameof() expression in ASP.NET MVC for controller names? Url.Action("ActionName", "Home") <------ works vs Url.Action(nameof(ActionName), nameof(HomeController)) <----- doesn't work obviously it doesn't…
Mikeon
  • 10,499
  • 6
  • 27
  • 31
48
votes
2 answers

Can C# nameof operator reference instance property without instance?

I regularly want to get the name of an instance property of a type, when I have no instance. Currently to do this, I use the following inhouse function which interprets the Expression[Func[T, object]] parameter and returns the property name: var str…
Brendan Hill
  • 3,406
  • 4
  • 32
  • 61
48
votes
6 answers

Why does nameof return only last name?

nameof(order.User.Age) return only Age instead of order.User.Age What is the reason to do it in more restricted way? If we want only last name we could do something like public static GetLastName(this string x) { return string.Split(x,…
ais
  • 2,514
  • 2
  • 17
  • 24
47
votes
6 answers

nameof equivalent in Java

C# 6.0 introduced the nameof() operator, that returns a string representing the name of any class / function / method / local-variable / property identifier put inside it. If I have a class like this: class MyClass { public SomeOtherClass…
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
40
votes
1 answer

Is there any way for the nameof operator to access method parameters (outside of the same method)?

Take the following class and method: public class Foo public Foo Create(string bar) { return new Foo(bar); } So getting "Create" is obvious: nameof(Foo.Create) Is there any way to get "bar" other than using reflection to read the…
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
40
votes
7 answers

Difference between nameof and typeof

Correct me if I am wrong, but doing something like var typeOfName = typeof(Foo).Name; and var nameOfName = nameof(Foo); should give you exactly the same output. One of the understandable reasons according to this source:…
user1400995
36
votes
1 answer

Get the name of a variable (like nameof operator in C#)

Let's assume we have the following code: const myVariable = { age: 7, name: "Hunter" }; I want to know the name of the variable, not just the value of it, and put it in a string or log it or: const s = nameof(myVariable) + ': ' +…
dr. rAI
  • 1,773
  • 1
  • 12
  • 15
35
votes
2 answers

ReSharper highlights use of nameof with "Explicit argument passed to parameter with caller info attribute"

I'm using the nameof function to get a property name as a string thus: public bool IsRunning => ...; ... RaisePropertyChanged(nameof(IsRunning)); ReSharper highlights this with the warning: Explicit argument passed to parameter with caller info…
Tim Rutter
  • 4,549
  • 3
  • 23
  • 47
32
votes
2 answers

Why can nameof not be used with alias-qualified types at the root level?

Imagine a type at the root namespace level (could be in the default global space, or could potentially be an extern alias). It appears that this type cannot be referred to via nameof(), when using the alias prefix. It works fine with typeof, and via…
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
28
votes
5 answers

nameof expression in .net framework 4

"nameof" expression is introduced in Visual Studio 2015 and c# 6 nameof (C# and Visual Basic Reference) How can u use it or write a similar method in older versions like .net framework 4.
MSL
  • 990
  • 1
  • 12
  • 28
27
votes
2 answers

Implicit and explicit typing with C# 6 nameof

One of the handiest new features in C# 6 is nameof, which allows the programmer to effectively eliminate the use of magic strings. Per the documentation, nameof returns a string: Used to obtain the simple (unqualified) string name of a variable,…
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
25
votes
2 answers

How to change language version in Visual Studio 2015

I want to use the nameof operator in my C# project in Visual Studio 2015 but the compiler complains with the following message. Feature 'nameof operator' is not available in C# 5. Please use language version 6 or greater. I want to know how I…
bittusarkar
  • 6,247
  • 3
  • 30
  • 50
1
2 3 4 5 6 7