我注意到了一些对我来说很奇怪的东西。显然,这是有意为之的,但是我想不出编译器为什么要这样做。
请考虑以下代码:
static void Main(string[] args)
{
int test_1 = 1;
int test_2 = 0;
int test_3 = 0;
if (test_1 == 1) Console.Write(1);
else if (test_2 == 1) Console.Write(1);
else if (test_3 == 1) Console.Write(2);
else Console.Write("x");
}
这段代码毫无意义,但我写它是为了看ILSpy如何解释if
语句。
然而,当我编译/反编译这段代码时,我注意到一件让我感到困惑的事情。我的第一个变量test_1
被优化为test_
!C#编译器这样做有没有好的理由呢?
为了全面检查,下面是我在ILSpy中看到的Main()
输出结果。
private static void Main(string[] args)
{
int test_ = 1; //Where did the "1" go at the end of the variable name???
int test_2 = 0;
int test_3 = 0;
if (test_ == 1)
{
Console.Write(1);
}
else
{
if (test_2 == 1)
{
Console.Write(1);
}
else
{
if (test_3 == 1)
{
Console.Write(2);
}
else
{
Console.Write("x");
}
}
}
}
更新
经过检查IL代码,发现这是ILSpy的问题,而不是C#编译器的问题。Eugene Podskal已经对我的初步评论和观察给出了很好的回答。然而,我想知道这是否是ILSpy内部的一个bug,还是刻意的功能。
dotPeek
显示变量名为num1
,num2
和num3
。 - Habib