C# 多重铸造示例
有人可以为我逐字写下这个内容,以便我能够了解选角是如何进行的吗?括号的数量让我感到困惑。
(Dictionary
只能找到简单的转换示例搜索(可能意味着我正在搜索错误的东西)不是很有帮助。
Could someone write this literally for me so I can understand how the casting is being carried out? The amount of brackets confuses me.
(Dictionary<String, String>)((Object[])e.Result)[1];
Was only able to find simple cast examples searching (possibly means I'm searching for wrong thing) which weren't very helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,e.Result 被转换为
Object
类型的数组,然后,该数组中索引 1 处的项目
[1]
被转换为Object
类型的字典。 code>希望有所帮助。
Firstly, e.Result is casted to an array of type
Object
Then, the item at index 1 in that array,
[1]
, is casted to a Dictionary of type<string, string>
Hope that helped.
为了理解操作的顺序,您需要记住强制转换将应用于右侧的对象,但影响何时应用的规则取决于 MSDN 的 运算符优先级和结合性
变为强制
转换操作被分组为“一元运算符”类别的优先级低于“主表达式”类别中的索引器 - []。
在原始行中 - () 在强制转换周围是必需的: ((Object[])e.Result) 因为索引器 - [] 作为第一优先级立即应用于左侧的对象。如果没有周围的 () ,则将在索引器之后应用强制转换,并且由于 e.Result 是(可能?)类型对象,因此这将在编译时失败。如果没有 (),该行将如下所示:
这将是无效的。
确保首先将 e.Result 转换为 Object[] 类型,然后使用索引器访问第一个元素。
第二次转换将转换后的 object[] (在我的示例中为转换 1)的第一个元素转换为字典
In order to understand the order of operations you need to remember that the cast will be applied to the object to the right but the rules that affect WHEN it is applied depends on MSDN's Operator precedence and associativity
Becomes
The cast operation is grouped into the "Unary Operators" category which is a lower priority than the indexer - [] which is in the "Primary expressions" category.
In the original line - The ()'s are necessary around the cast: ((Object[])e.Result) because the indexer - [] is applied immediately to the object on the left as the first priority. Without the surrounding ()'s the cast would be applied AFTER the indexer and since e.Result is (likely?) of type object this will fail at compile time. Without the ()'s the line would look like:
Which would not be valid.
ensures that e.Result is cast to type Object[] first, and then the indexer is used to access the first element.
The second cast turns the first element of the casted object[] (in my example cast 1) into a Dictionary