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

1

11

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;
    }
}
Aegis answered 7/5, 2015 at 9:58 Comment(2)
I don't think nameof(MyObject.MyProperty) is going to work. MyProperty is not static. You would need an instance of MyObject.Gusty
It is. The property name is always static @FrédéricHamidiOstrander
O
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!

Ostrander answered 7/5, 2015 at 10:2 Comment(7)
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.)Aimee
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.Aimee
@O.R.Mapper If you don't believe Patrick, you can double check nameof here: tryroslyn.azurewebsites.netCodify
@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.Aimee
@O.R.Mapper as this answer shows it works. And property names are constants.Ostrander
@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.Aimee
@O.R.Mapper okay. Got it.Ostrander

© 2022 - 2024 — McMap. All rights reserved.