What is the difference between MyEnum.Item.ToString() and nameof(MyEnum.Item)?

后端 未结 3 1142
南旧
南旧 2020-12-20 14:17
MyEnum.Item.ToString();
nameof(MyEnum.Item);

Which style is preferred? Is there any practical difference between the two?

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 14:37

    If you're not obfuscating your code, pretty much the same... the moment you obfuscate your code however, ToString() will likely produce garbage, while nameof() will retrieve the name you requested / expected.

    More info on MSDN

提交回复
热议问题