展望的源代碼Microsoft.VisualBasic.Strings.Chr()
,我看到以下(我已經簡化了此篇通過除去異常處理):
/// <summary>
/// Returns the character associated with the specified character code.
/// </summary>
///
/// <returns>
/// Returns the character associated with the specified character code.
/// </returns>
/// <param name="CharCode">Required. An Integer expression representing the code point, or character code, for the character.</param><exception cref="T:System.ArgumentException"><paramref name="CharCode"/> < 0 or > 255 for Chr.</exception><filterpriority>1</filterpriority>
public static char Chr(int CharCode)
{
if (CharCode <= (int) sbyte.MaxValue)
return Convert.ToChar(CharCode);
Encoding encoding = Encoding.GetEncoding(Utils.GetLocaleCodePage());
char[] chars1 = new char[2];
byte[] bytes = new byte[2];
Decoder decoder = encoding.GetDecoder();
int chars2;
if (CharCode >= 0 && CharCode <= (int) byte.MaxValue)
{
bytes[0] = checked ((byte) (CharCode & (int) byte.MaxValue));
chars2 = decoder.GetChars(bytes, 0, 1, chars1, 0);
}
else
{
bytes[0] = checked ((byte) ((CharCode & 65280) >> 8));
bytes[1] = checked ((byte) (CharCode & (int) byte.MaxValue));
chars2 = decoder.GetChars(bytes, 0, 2, chars1, 0);
}
return chars1[0];
}
這似乎是,對於7位值,Convert.ToChar(CharCode)
被返回,我猜測編譯器足夠聰明,可以得出結論是一個常量,而對於8位值的當前文化的CodePage會涉及到,這將給出基於計算機運行代碼的不同結果,因此不能一個常數。
更新:我試圖複製我自己編寫的方法中的情況,但不能,這表明編譯器本身可能有一個用於評估常量表達式的特殊情況規則。
Private Function ConstOrNot(input As Int32) As Int32
If input = 3 Then Return 7
Return (New Random).Next
End Function
Const intC1 As Int32 = ConstOrNot(3)
(這就是說,ConstOrNot()
存在於同一個組件的代碼調用它,所以這可能不會反正工作。)
+1有趣的問題。我很確定它與172在標準ASCII表之外有什麼關係(它是7位,我期望你的問題發生在高於127的值),但我不知道爲什麼它會打擾編譯器。 –
良好的洞察力。它似乎與8位值有關,因爲樣本值122和127被接受爲常量表達式,但128和172不是。 – MCattle
+1有趣的問題 – SSS