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

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

问题描述

如果 intInt32 同义,为什么

If int is synonymous to Int32 why does

enum MyEnum : Int32
{
    Value = 1
}

...不编译?哪里

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"...等).解析器需要来自 integral-types 语法规则的特定字符串.

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.

错误是解析错误,即使 enum Enum : intenum Enum : 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天全站免登陆