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

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

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

3条回答
  •  萌比男神i
    2020-12-20 14:50

    .ToString() is evaluated at runtime and can be called with your own format.

    nameof() is evaluated at compile-time and will inject a string literal that will never change.

提交回复
热议问题