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
24
votes
2 answers

Is there any benefit of using the nameof operator instead of the CallerMemberNameAttribute to notify property changes in .NET 4.5.3?

With the advent of .NET 4.5.3, WPF developers now have three (or more) ways to notify the INotifyPropertyChanged Interface of property changes. Basically, my question is Which of the two methods introduced from.NET 4.5 onwards is the more efficient…
Sheridan
  • 68,826
  • 24
  • 143
  • 183
24
votes
5 answers

How to handle nameof(this) to report class name

I'd like to use the following C#6 code var joe = new Self(); Console.WriteLine(joe); ... and get the following output: joe The following attempt class Self { public string Name { get; set; } = nameof(this); public override string ToString()…
nincsmail
  • 241
  • 1
  • 2
  • 4
13
votes
2 answers

Is there a Swift equivalent of C#'s 'nameof()' function to get a variable or member's name at compile time?

Ok, there's an existing question here on S/O with the following title: Swift: Get Variable Actual Name as String By it's name, it seems that's exactly what I want. However, looking at the accepted answer (and the other non-accepted ones), they are…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
13
votes
2 answers

C# 6: nameof() current property in getter/setter

Is there a way to get the name of the current property in a getter/setter? Something like this: public string MyProperty { get { return base.Get(nameof(ThisProperty)); } set { base.Set(nameof(ThisProperty), value);…
evaenrique
  • 981
  • 2
  • 8
  • 17
10
votes
3 answers

Declaring constants with the nameof() the constant as the value

Scenario I have a class for declaring string constants used around a program: public static class StringConstants { public const string ConstantA = "ConstantA"; public const string ConstantB = "ConstantB"; // ... } Essentially, it…
Geoff James
  • 3,122
  • 1
  • 17
  • 36
10
votes
2 answers

Parsing nameof expressions in Roslyn

I'm trying to do something with nameof expressions in a CSharpSyntaxWalker, however, I noticed that there is no NameOfExpressionSyntax in the AST. Instead I get an InvocationExpressionSyntax for which SemanticModel.GetSymbolInfo returns no matching…
Joey
  • 344,408
  • 85
  • 689
  • 683
10
votes
1 answer

Is it possible to use an nameof expression in switch statement?

The new C# 6.0 nameof is great in the PropertyChanged pattern for propagating property changes using something like: private string _myProperty; public string MyProperty { get { return _myProperty; } set { …
Rogier
  • 1,170
  • 1
  • 10
  • 21
9
votes
1 answer

Why is nameof(object) not allowed?

In C# 6.0 you can write this: var instance = default(object); var type = typeof(object); They have the same result of: var instance = default(System.Object); var type = typeof(System.Object); But you can't write this: var name =…
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
9
votes
1 answer

Use nameof on a member of a generic class without specifying type arguments

class Foo { public T Bar() { /* ... */ } } I'd like to pass Bar's name to Type.GetMethod(string). I can do this as someType.GetMethod(nameof(Foo.Bar)), but that int is wholly arbitrary here; is there any way I can omit it? Sadly,…
dlf
  • 9,045
  • 4
  • 32
  • 58
8
votes
1 answer

Is it possible to imply the name of the parameters of a params array using the nameof operator?

I thought I could make use of the new c# 6 operator nameof to build a dictionary of key/values implicitly from a params array. As an example, consider the following method call: string myName = "John", myAge = "33", myAddress =…
Veverke
  • 9,208
  • 4
  • 51
  • 95
8
votes
2 answers

Getting the calling variable name of a parameter

In relation to the question Get the name of parameters from a calling method and Finding the Variable Name passed to a Function in C# I'm still looking for a way to define theWhatDoesTheAnimalSay_WANTED method: I want to know the name of the…
Soko
  • 774
  • 1
  • 7
  • 20
7
votes
1 answer

F# nameof operator not a first-class function

I'm using F# 4.7 with preview in my project file. I have a type like this: type Record = { Name : string Description : string FieldNotInterestedIn: int } I'd like to get the names of certain fields in a type-safe…
Brett Rowberry
  • 1,030
  • 8
  • 21
6
votes
3 answers

Null-check multiple parameters and throw an exception with their name

I would like to validate multiple parameters and throw an ArgumentNullException if any of them are null. For the sake of argument, let's assume I've got this: public void DoSomething(SomeClass param1, SomeClass param2, SomeClass param3); Of course,…
user622505
  • 743
  • 6
  • 23
6
votes
4 answers

Expression vs nameof

It is a good idea to use nameof over expressions for extracting property names? //method with expression protected void RaisePropertyChanged(Expression> propertyExpression, bool isValid, [param: Localizable(true)] string…
isxaker
  • 8,446
  • 12
  • 60
  • 87
5
votes
2 answers

nameof won´t reflect using

I have an alias within my source-code file like this: MyClass.cs using System; using SomeClass = NewClass; public class Program { public static void Main() { Console.WriteLine(nameof(SomeClass)); } } public…
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111