为什么“int []是uint [] == true”在C# [英] Why does "int[] is uint[] == true" in C#

查看:155
本文介绍了为什么“int []是uint [] == true”在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以澄清C#关键字。特别是这两个问题:

Can somebody clarify the C# is keyword please. In particular these 2 questions:

Q1)第5行;为什么这会返回true?

Q1) line 5; Why does this return true?

Q2)line 7;为什么没有转义异常?

Q2) line 7; Why no cast exception?

public void Test()
{
    object intArray = new int[] { -100, -200 };            

    if (intArray is uint[]) //why does this return true?
    {
        uint[] uintArray = (uint[])intArray; //why no class cast exception?

        for (int x = 0; x < uintArray.Length; x++)
        {
            Console.Out.WriteLine(uintArray[x]);
        }
    }
}

MSDN的说明情况。它表示将返回true,如果满足这些条件之一。 (http://msdn.microsoft.com/en-us/library/scekt9xw(VS.71).aspx>MDSN文章)

MSDN's description does not clarify the situation. It states that is will return true if either of these conditions are met. (http://msdn.microsoft.com/en-us/library/scekt9xw(VS.71).aspx>MDSN Article)


expression is not null.
expression can be cast to type.

我不相信你可以做一个有效的int []到uint []。因为:

I don't believe that you can do a valid cast of int[] into uint[]. Because:

A)此代码不编译:

int[] signed = new int[] { -100 };
uint[] unsigned = (uint[])signed; 

B)在调试器中执行转换会产生错误:

B) Doing the cast in the debugger gives an error:

(uint[])signed
"Cannot convert type 'int[]' to 'uint[]'"

没错,如果第3行是int []而不是object,那么它永远不会编译。

Sure enough, if line 3 was int[] instead of object then it would never compile. Which brings me to a final question related to Q2.

Q3)为什么C#在调试器和编译器中引发了转换/转换错误,而在运行时却没有?

Q3) Why does C# raise a cast/conversion error in the debugger and compiler but not at runtime?

推荐答案

C#和CLR有一些不同的转换规则。

C# and the CLR have somewhat different conversion rules.

在C#中的 int [] uint [] 之间直接 >语言不相信有任何转换可用。但是,如果你通过 object ,结果是由CLI。从CLI规范第8.7节(我希望 - 我引用

You can't directly cast between int[] and uint[] in C# because the language doesn't believe any conversion is available. However, if you go via object the result is up to the CLI. From the CLI spec section 8.7 (I hope - I'm quoting an email exchange I had on this topic with Eric Lippert a while ago):


签署和无符号整数原语
类型可以彼此分配;
例如,int8:= uint8有效。对于此
目的,bool应被视为
兼容 uint8 ,反之亦然,
使 bool: = uint8 有效,
反之亦然。这同样适用于
相同大小的有符号和无符号整数
原始类型的数组;
eg, int32 []:= uint32 [] 有效。

Signed and unsigned integral primitive types can be assigned to each other; e.g., int8 := uint8 is valid. For this purpose, bool shall be considered compatible with uint8 and vice versa, which makes bool := uint8 valid, and vice versa. This is also true for arrays of signed and unsigned integral primitive types of the same size; e.g., int32[] := uint32[] is valid.

(我没有检查,但我认为这种类型的引用类型转换是有效的是也会返回true。)

(I haven't checked, but I assume that this sort of reference type conversion being valid is what makes is return true as well.)

有些不幸的是,语言和底层执行引擎之间存在断开连接,但是从长远来看,这是非常不可避免的,我怀疑。还有一些其他的情况下这样,但好消息是,他们很少似乎造成重大损害。

It's somewhat unfortunate that there are disconnects between the language and the underlying execution engine, but it's pretty much unavoidable in the long run, I suspect. There are a few other cases like this, but the good news is that they rarely seem to cause significant harm.

编辑:因为马克删除了他的答案,我链接来自Eric的完整邮件,发布到C#新闻组。

As Marc deleted his answer, I've linked to the full mail from Eric, as posted to the C# newsgroup.

这篇关于为什么“int []是uint [] == true”在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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