C#int,Int32和枚举 [英] C# int, Int32 and enums

查看:180
本文介绍了C#int,Int32和枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 int Int32 的同义词为什么

enum MyEnum : Int32
{
    Value = 1
}

...不编译?在哪里

...not compile? Where as

enum MyEnum : int
{
    Value = 1
}

即使将光标悬停在int字上也会显示struct System.Int32?

will, even though hovering the cursor over the int word will display struct System.Int32?

推荐答案

底层类型确实是一样的,但编译器依赖于类型作为确切的别名。这是基于解析的编译错误。我看了一下C#语法规范和基于别名定义的底层类型(例如int,unit...等等)。解析器期望从整体类型语法规则中的特定字符串。

The underlying type is indeed the same, but the compiler depends on the type to be as the exact alias. This is a compilation error based on parsing. I took a look at C# grammar specification and the underlying types defined there as tokens based on the alias (e.g. 'int', 'unit'... etc.). The parser expects specific strings from the integral-types grammar rule.

即使是错误也是解析错误枚举枚举枚举:int 与枚举枚举枚举相同:Int32

The error is a parsing error even though both enum Enum : int means the same as enum Enum : Int32.

我不知道强制这个限制解析步骤的原因,但是我可以尝试猜测:由于Int32不是一个关键字,它可能指的是其他实际的int结构。如果解析器必须知道类型,以便为每个基本类型构建不同的 AST ,那么它不能取决于不是关键字的令牌。

I don't know the reason for forcing this limit to parsing step, but I can try guessing: Since Int32 is not a keyword it might refer to something other the actual int struct. If the parser has to know the type in order to build different AST for each base type then it cannot depend on token which is not a keyword.

即使C#规范将 int 关键字定义为显式别名System.Int32 ,在解析步骤中获取有关显式类型(Int32)的此信息仍然是一个问题,因为它需要大量不能在此步骤推断的上下文信息。

Even though the C# specification defines the int keyword as explicit alias System.Int32, it's still a problem to get this information about the explicit type (Int32) during parsing step since it requires a lot of context information which cannot be inferred at this step.

这篇关于C#int,Int32和枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆