声明十六进制数:当前上下文中不存在名称 'B9780'

Declaring hex number: The name 'B9780' does not exist in the current context

我不知道为什么会这样说:

The name 'B9780' does not exist in the current context

我有以下代码:

class Program
{
    public static int TimerBase = 00DB9780;
}

我希望它能接受,但它说:

Cannot implicitly convert type double to int. An explicit conversion exists (are you missing a cast?)

00DB9780表示为十六进制。将值声明为 int 前缀 0x:

public static int TimerBase = 0x00DB9780;
                          //  ^^ add 0x to start of hex numbers  

前缀 0x 告诉编译器期望(并解析)一个十六进制数。


错误解释

编译器说不能将 int 转换为 double 的原因是值 00D 的第一部分实际上是在 C# 中声明 double 的一种方法。有关详细信息,请参阅 Real literals。所以编译器将你的意图解释为:

public static int TimerBase = 0D;  // LHS is int, RHS is double

所以你得到错误:

cannot implicitly convert type double to int

编译器希望在 double 声明 (00D) 之后看到 ;,因此它还会显示缺少分号的错误:

; expected

接下来,编译器看到 B9780,它试图将其解释为一个变量,这就是您收到错误的原因:

'B9780' does not exist in the current context