【问题标题】:Is it possible to overload the nameof operator in c#?是否可以在 c# 中重载 nameof 运算符?
【发布时间】:2017-12-14 10:53:23
【问题描述】:

我可以在 C# 中重载 nameof 运算符吗?

C# programming guide 已过时,C# 6 under the hood 对我没有帮助。

如何重载nameof 运算符?

【问题讨论】:

  • nameof 在编译时进行评估,因此重载的概念对我来说没有多大意义

标签: c# operator-overloading c#-6.0


【解决方案1】:

nameof 运算符在编译时进行评估。

nameof 表达式是一个常量。在所有情况下,nameof(...) 在编译时被评估以产生一个字符串。它的参数在运行时不会被评估,并且被认为是不可访问的代码(但是它不会发出“不可访问代码”警告)。

所以,这些运算符不能重载。

【讨论】:

  • 你在引用什么?使用引用时添加引用(名称、链接)是有意义的。
  • 我不知道@solid-fan 是从哪里引用的,但这里是微软documentation
猜你喜欢
  • 2012-09-04
  • 1970-01-01
  • 2014-02-22
  • 2015-01-01
  • 2010-10-20
  • 2010-10-21
  • 2017-02-05
  • 2012-01-12
  • 2017-05-06
相关资源
最近更新 更多