为什么来自 C# 8 数组切片功能的新帽子运算符索引不从 0 开始? [英] Why doesn't the new hat-operator index from the C# 8 array-slicing feature start at 0?

查看:15
本文介绍了为什么来自 C# 8 数组切片功能的新帽子运算符索引不从 0 开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C# 8.0 引入了一种对数组进行切片的便捷方法 - 参见 官方 C# 8.0 博文.

C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost.

访问数组最后一个元素的语法是

The syntax to access the last element of an array is

var value = new[] { 10, 11, 12, 13 };

int a = value[^1]; // 13
int b = value[^2]; // 12

我想知道为什么反向访问元素的索引从 1 而不是 0 开始?这有技术原因吗?

I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this?

推荐答案

官方解答

为了更好的可见性,这里是 Mads Torgersen 的评论,解释了 C# 8 博文:

当涉及到从头到尾的算术时,我们决定遵循 Python.0 指定第一个元素(和往常一样),^0 指定length'th"元素,即末尾的元素.这样你就得到了一个简单的关系,其中一个元素从开始的位置加上它从结束的位置等于长度.^x 中的 x 是如果您自己进行数学计算会从长度中减去的值.

We decided to follow Python when it comes to the from-beginning and from-end arithmetic. 0 designates the first element (as always), and ^0 the "length’th" element, i.e. the one right off the end. That way you get a simple relationship, where an element's position from beginning plus its position from end equals the length. the x in ^x is what you would have subtracted from the length if you’d done the math yourself.

为什么不使用减号 (-) 而不是新帽子 (^) 运算符?这主要与范围有关.再次与 Python 和大多数行业保持一致,我们希望我们的范围在开始时是包容的,在结束时是独占的.你传递的索引是什么,表示一个范围应该一直走到最后?在 C# 中,答案很简单:x..^0x 到结尾.在 Python 中,您无法给出明确的索引:-0 不起作用,因为它等于 0,第一个元素!因此,在 Python 中,您必须完全关闭结束索引以表示到达结尾的范围:x...如果计算范围的末尾,那么您需要记住有特殊的逻辑,以防它出现0.就像在 x..-y 中一样,其中 y 被计算出来并得到 0.这是一个常见的麻烦和错误的来源.

Why not use the minus (-) instead of the new hat (^) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple: x..^0 goes from x to the end. In Python, there is no explicit index you can give: -0 doesn’t work, because it is equal to 0, the first element! So in Python, you have to leave the end index off completely to express a range that goes to the end: x... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0. As in x..-y, where y was computed and came out to 0. This is a common nuisance and source of bugs.

最后,请注意索引和范围是 .NET/C# 中的第一类类型.它们的行为与它们所应用的内容无关,甚至与索引器中使用的内容无关.你可以完全定义你自己的索引器,它接受 Index 和另一个接受 Range 的索引器——我们将把这样的索引器添加到例如跨度.但是,例如,您也可以使用带范围的方法.

Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to be used in an indexer. You can totally define your own indexer that takes Index and another one that takes Range – and we’re going to add such indexers to e.g. Span. But you can also have methods that take ranges, for instance.

我的回答

我认为这是为了匹配我们习惯的经典语法:

My answer

I think this is to match the classic syntax we are used to:

value[^1] == value[value.Length - 1]

如果使用 0,当这两种语法并排使用时会造成混淆.这样,它降低认知负荷.

If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.

Python 等其他语言也使用相同的约定.

Other languages like Python also use the same convention.

这篇关于为什么来自 C# 8 数组切片功能的新帽子运算符索引不从 0 开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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