10

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
    {
        _myProperty= value;
        OnPropertyChanged(nameof(MyProperty));
    }
}

When listening of property changes I use this (yes, even with ugly hardcoded strings):

    private void OnMyObjectPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        switch (args.PropertyName)
        {
            case "MyProperty":
                DoSomething();
                break;
        }
    }

With the new nameof expressions would this code compile / work?

private void OnMyObjectPropertyChanged(object sender, PropertyChangedEventArgs args)
{
    switch (args.PropertyName)
    {
        case nameof(MyObject.MyProperty):
            DoSomething();
            break;
    }
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Rogier
  • 1,170
  • 1
  • 10
  • 21

1 Answers1

16

According to this question, the evaluation of the nameof keyword is done on compile time. This will make it a constant, which will work inside switch statements.

This is proven when you look to the compiled output of this code:

using System;

public class Program
{
    public string A { get; set; }

    public static void Main()
    {
        string a = "A";

        switch (a)
        {
            case nameof(Program.A):
            {
                Console.WriteLine("Yes!");
                break;
            }
        }

    }
}

Output:

Yes!

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    This does not answer yet whether it also works with non-static members, and - maybe more importantly - properties. The syntax for identifying methods like `Program.Main` was already existing, for example, to identify method passed to event handlers. However, is something like `Program.SomeProperty` possible, as well? (Based on a slight extension of your Fiddle, it seems it is, but I think it should be added as properties are what was asked for.) – O. R. Mapper May 07 '15 at 10:11
  • I have become careful with "of courses"; the signature of a particular method overload is a constant, as well, and yet, there is no C# syntax for expressing it, [which is a major reason why we're just getting `nameof` instead of an `infoof` operator](http://blogs.msdn.com/b/ericlippert/archive/2009/05/21/in-foof-we-trust-a-dialogue.aspx). – O. R. Mapper May 07 '15 at 10:17
  • 1
    @O.R.Mapper If you don't believe Patrick, you can double check `nameof` here: http://tryroslyn.azurewebsites.net/ – JoshVarty May 07 '15 at 17:49
  • @JoshVarty: As I have written in my comment, I have already verified the claim 8 hours ago. However, it was essential that this answer actually show that `nameof` can be used on properties the way the OP asked, rather than just showing it can be used on methods and concluding that it is certainly possible for properties because "property names are constant". Many code elements are constant, but only some can be *identified* in valid C# code. – O. R. Mapper May 07 '15 at 18:34
  • @O.R.Mapper as this answer shows it works. And property names are constants. – Patrick Hofman May 07 '15 at 18:38
  • @PatrickHofman: Yes, since properties were included in the answer, it does show that :) And, yes, of course property names are constants, I never claimed anything else - it's just that the answer to the question cannot be concluded from the fact that property names are constants. – O. R. Mapper May 07 '15 at 18:48
  • 1
    @O.R.Mapper okay. Got it. – Patrick Hofman May 07 '15 at 18:51