此代码不能与latest C# compiler一起编译:
public class Program
{
public static void Main()
{
IntEnum a = (IntEnum)-1;
}
}
public enum IntEnum : int { }
当你试图编译它时,它会引发
(3,22,3,29): Error CS0119: ‘IntEnum’ is a type, which is not valid in the given context
奇怪的是,将转换值更改为正数(例如4),或使用const值(例如int.MinValue),或者甚至用括号如(IntEnum)( – 1)包围值将编译并正常工作.但是,上述样本没有.
这有什么理由吗?是否有可能Roslyn可能错误地解析了代码,这就是为什么错误会被提升?
最佳答案:
期望行为并记录以允许解析像(Var)-1这样的表达式.
Compiler Error CS0075进入规范细节(我希望你能得到这个错误/除了CS0119):
To cast a negative value, you must enclose the value in parentheses
If you are casting using a keyword that identifies a predefined type, then you do not need parentheses. Otherwise, you must put the parentheses because (x) –y will not be considered a cast expression. From the C# Specification, Section 7.6.6:From the disambiguation rule it follows that, if x and y are identifiers, (x)y, (x)(y), and (x)(-y) are cast-expressions, but (x)-y is not, even if x identifies a type. However, if x is a keyword that identifies a predefined type (such as int), then all four forms are cast-expressions (because such a keyword could not possibly be an expression by itself).
标签:c,casting,net,enums,roslyn 来源: https://codeday.me/bug/20190516/1115049.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。